Easyadminbundle: Forms generate wrong markup because of the "form groups" feature

Created on 1 Aug 2016  路  4Comments  路  Source: EasyCorp/EasyAdminBundle

If you look at the HTML of the fields included in the forms, you see:

<input type="text" value="..." class="form-control" 
       form_group="_easyadmin_form_design_element_0" ...>

This form_group attribute comes from this line:

$formFieldOptions['attr']['form_group'] = $currentFormGroup;

The template needs this information to know where/how to render each form field, but the field themselves don't need that information.


What's the best way to solve this?

  • Replace form_group name by data-form-group ?
  • Refactor everything using some complex form feature?
  • ...
bug hasPR

Most helpful comment

@javiereguiluz : You can use something like this:

diff --git a/Form/Extension/EasyAdminExtension.php b/Form/Extension/EasyAdminExtension.php
index f7c7433..85bbd37 100644
--- a/Form/Extension/EasyAdminExtension.php
+++ b/Form/Extension/EasyAdminExtension.php
@@ -62,6 +62,7 @@ class EasyAdminExtension extends AbstractTypeExtension
                 'view' => $action,
                 'item' => $easyadmin['item'],
                 'field' => isset($fields[$view->vars['name']]) ? $fields[$view->vars['name']] : null,
+                'form_group' => $form->getConfig()->getAttribute('easyadmin_form_group'),
             );
         }
     }
diff --git a/Form/Type/EasyAdminFormType.php b/Form/Type/EasyAdminFormType.php
index 025fb27..bbc38a9 100644
--- a/Form/Type/EasyAdminFormType.php
+++ b/Form/Type/EasyAdminFormType.php
@@ -81,8 +81,6 @@ class EasyAdminFormType extends AbstractType
                 continue;
             }

-            $formFieldOptions['attr']['form_group'] = $currentFormGroup;
-
             // 'divider' and 'section' are 'fake' form fields used to create the design
             // elements of the complex form layouts: define them as unmapped and non-required
             if (0 === strpos($metadata['property'], '_easyadmin_form_design_element_')) {
@@ -90,7 +88,10 @@ class EasyAdminFormType extends AbstractType
                 $formFieldOptions['required'] = false;
             }

-            $builder->add($name, $formFieldType, $formFieldOptions);
+            $child = $builder->getFormFactory()->createNamedBuilder($name, $formFieldType, null, $formFieldOptions);
+            $child->setAttribute('easyadmin_form_group', $currentFormGroup);
+
+            $builder->add($child);
         }

         $builder->setAttribute('easyadmin_form_groups', $formGroups);

All 4 comments

I guess we have two options:

  1. The data-form-group is only well suited if you need to keep control on the frontend side with JavaScript for instance.
  2. We can pass this information to the view with the buildView() method, so that we can get it as an auto declared Twig variable in the Twig blocks. We'll also have it in the form.vars array.

馃憤 This should totally belong to the FormView vars.

@javiereguiluz : You can use something like this:

diff --git a/Form/Extension/EasyAdminExtension.php b/Form/Extension/EasyAdminExtension.php
index f7c7433..85bbd37 100644
--- a/Form/Extension/EasyAdminExtension.php
+++ b/Form/Extension/EasyAdminExtension.php
@@ -62,6 +62,7 @@ class EasyAdminExtension extends AbstractTypeExtension
                 'view' => $action,
                 'item' => $easyadmin['item'],
                 'field' => isset($fields[$view->vars['name']]) ? $fields[$view->vars['name']] : null,
+                'form_group' => $form->getConfig()->getAttribute('easyadmin_form_group'),
             );
         }
     }
diff --git a/Form/Type/EasyAdminFormType.php b/Form/Type/EasyAdminFormType.php
index 025fb27..bbc38a9 100644
--- a/Form/Type/EasyAdminFormType.php
+++ b/Form/Type/EasyAdminFormType.php
@@ -81,8 +81,6 @@ class EasyAdminFormType extends AbstractType
                 continue;
             }

-            $formFieldOptions['attr']['form_group'] = $currentFormGroup;
-
             // 'divider' and 'section' are 'fake' form fields used to create the design
             // elements of the complex form layouts: define them as unmapped and non-required
             if (0 === strpos($metadata['property'], '_easyadmin_form_design_element_')) {
@@ -90,7 +88,10 @@ class EasyAdminFormType extends AbstractType
                 $formFieldOptions['required'] = false;
             }

-            $builder->add($name, $formFieldType, $formFieldOptions);
+            $child = $builder->getFormFactory()->createNamedBuilder($name, $formFieldType, null, $formFieldOptions);
+            $child->setAttribute('easyadmin_form_group', $currentFormGroup);
+
+            $builder->add($child);
         }

         $builder->setAttribute('easyadmin_form_groups', $formGroups);

@ogizanagi thanks! I'll give that a try.

Was this page helpful?
0 / 5 - 0 ratings