title: Symfony2 double nested dynamic form fields
body: <p>I have a Symfony2 Form with two layered dynamic fields. The first Layer is no problem implementing the documented way with form events: <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms" rel="noreferrer">http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms</a></p>
  
  <p>But then comes a third field, which depends on the second field, which is already a dynamic field.</p>
  
  <p>To demonstrate the problem, here is my stripped code:</p>
  
  <pre><code>&lt;?php
  class ServiceeventType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('park', 'entity', array(
                  'class' =&gt; 'AppBundle:Park',
                  'property' =&gt; 'identifyingName',
                  'label' =&gt; 'Park',
                  'required' =&gt; true,
                  'invalid_message' =&gt; 'Choose a Park',
                  'placeholder' =&gt; 'Please choose',
              ))
              // just a placeholder for the $builder-&gt;get('facility')-&gt;addEventListener to have something to bind to
              // I'm aware, that this is just a symptom of my problem
              -&gt;add('facility', 'choice', array(
                  'choices' =&gt; array(),
                  'expanded' =&gt; true,
                  'multiple' =&gt; false,
                  'label' =&gt; 'Facility',
                  'required' =&gt; false,
                  'invalid_message' =&gt; 'Choose a Park first',
                  'placeholder' =&gt; 'Please choose a Park first',
              ))
              // other fields
          ;
  
          $formModifierPark = function (FormInterface $form, Park $park = null) {
              // overwrite the facility field with the desired entity type
              $form-&gt;add('facility', 'entity', array(
                  'class' =&gt; 'AppBundle:Facility',
                  'property' =&gt; 'identifyingName',
                  'choices' =&gt; null === $park ? array() : $park-&gt;getFacilities(),
                  'label' =&gt; 'Facility',
                  'required' =&gt; true,
                  'invalid_message' =&gt; 'Choose a Facility',
                  'placeholder' =&gt; null === $park ? 'Please choose a Park first' : 'Please choose',
              ));
          };
  
          $formModifierFacility = function (FormInterface $form, Facility $facility = null) {
              $form-&gt;add('facilityStatuscode', 'entity', array(
                  'class' =&gt; 'AppBundle:FacilityStatuscode',
                  'property' =&gt; 'identifyingName',
                  'choices' =&gt; null === $facility ? array() : $facility-&gt;getFacilityStatuscodeType()-&gt;getFacilityStatuscodes(),
                  'label' =&gt; 'Statuscode',
                  'required' =&gt; null === $facility ? false : true,
                  'invalid_message' =&gt; 'Choose a Statuscode',
                  'placeholder' =&gt; null === $facility ? 'Please choose a Facility first' : 'Please choose',
              ));
          };
  
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($formModifierPark) {
                  $formModifierPark($event-&gt;getForm(), $event-&gt;getData()-&gt;getPark());
              }
          );
          $builder-&gt;get('park')-&gt;addEventListener(
              FormEvents::POST_SUBMIT,
              function (FormEvent $event) use ($formModifierPark) {
                  $formModifierPark($event-&gt;getForm()-&gt;getParent(), $event-&gt;getForm()-&gt;getData());
              }
          );
  
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($formModifierFacility) {
                  $formModifierFacility($event-&gt;getForm(), $event-&gt;getData()-&gt;getFacility());
              }
          );
          $builder-&gt;get('facility')-&gt;addEventListener(
              FormEvents::POST_SUBMIT,
              function (FormEvent $event) use ($formModifierFacility) {
                  $formModifierFacility($event-&gt;getForm()-&gt;getParent(), $event-&gt;getForm()-&gt;getData());
              }
          );
      }
  
      // more code
  }
  </code></pre>
  
  <p>The problem is now:</p>
  
  <p>The event-listener set with <code>$builder-&gt;get('facility')-&gt;addEventListener(FormEvents::POST_SUBMIT,…</code> gets lost at the moment, the facility-field is overwritten by the other event-listener.</p>
  
  <p>I tried several workarounds, but it turns out, that form field options cannot be overridden and form later added fields don't accept new event listeners, once the builder is ready (i.e. when added inside an event listener).</p>
  
  <p>I really have to solve this. Am I missing something? Is Symfony2's Form engine not able to handle two layered dynamic form field dependencies?</p>
  
  <p>Any suggestions?</p>