Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony Form Field Order when using Form Events
    text
    copied!<p>I am creating a dynamic form similar to what is wanted <a href="https://stackoverflow.com/questions/10186185/symfony2-chained-selectors/12052574#12052574">here</a>.</p> <p>Well i managed to get it working. But i would like that the native fields of the type could appear AFTER the dynamic dropdowns. This is not out of box because dropboxes are added to the form in the PRE_SET_DATA event (after the form is build and name field is added) as you can see:</p> <pre><code>public function buildForm(FormBuilder $builder, array $options) { $builder-&gt;add('country', 'entity', array( 'class'=&gt;'TestBundle:Country', 'property'=&gt;'name', 'property_path'=&gt;false //Country is not directly related to City )); $builder-&gt;add('name'); $factory = $builder-&gt;getFormFactory(); (...) $builder-&gt;addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($refreshStates, $setCountry) { $form = $event-&gt;getForm(); $data = $event-&gt;getData(); if($data == null) return; if($data instanceof City){ $country = ($data-&gt;getId()) ? $data-&gt;getState()-&gt;getCountry() : null ; $refreshStates($form, $country); $setCountry($form, $country); } }); </code></pre> <p>I tried to do the same for the name field adding this to the beginning of buildForm Function:</p> <pre><code>$addBaseFields = function ($form) use ($factory) { $form-&gt;add($factory-&gt;createNamed('text', 'name')); }; </code></pre> <p>and then i added it in the event:</p> <pre><code> $builder-&gt;addEventListener(FormEvents::PRE_SET_DATA, function (DataEvent $event) use ($refreshStates, $setCountry) { $form = $event-&gt;getForm(); $data = $event-&gt;getData(); if($data == null) return; if($data instanceof City){ $country = ($data-&gt;getId()) ? $data-&gt;getState()-&gt;getCountry() : null ; $refreshStates($form, $country); $setCountry($form, $country); $addBaseFields($form); } }); </code></pre> <p>This kind of works. But, html5 field validation stopped working. For example: if i define name as 'number' type instead of 'text': <code>$form-&gt;add($factory-&gt;createNamed('number', 'name'));</code> it will accept anything, any text.</p> <p>Is there a better way to do this so i can still use number browser validation?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload