title: Symfony2 Form - Event listener on Embed Form
body: <p>Starting from the example in the symfony cookbook about dynamic form generation (<a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data" rel="nofollow">link to doc</a>)</p>
  
  <pre><code>class SportMeetupType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('sport', 'entity', array(...))
          ;
  
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function(FormEvent $event) {
                  $form = $event-&gt;getForm();
  
                  // this would be your entity, i.e. SportMeetup
                  $data = $event-&gt;getData();
  
                  $positions = $data-&gt;getSport()-&gt;getAvailablePositions();
  
                  $form-&gt;add('position', 'entity', array('choices' =&gt; $positions));
              }
          );
      }
  }
  </code></pre>
  
  <p>I reproduced this code with a difference, I embed this form in a parent form (called for the exemple <code>SportPlayerType</code>).</p>
  
  <p><code>SportPlayerType</code> is mapped on entity <code>SportPlayer</code> which contains a couple of attributes : $name and $meetup, a string and a SportMeetup.</p>
  
  <p>My problem is in the lambda function, parameter of the addEventListener :</p>
  
  <ul>
  <li><code>$event-&gt;getForm()</code> returns the child form SportMeetupType</li>
  <li><code>$event-&gt;getData()</code> returns the parent mapped entity SportPlayer</li>
  </ul>
  
  <p>As a result, <code>$form-&gt;add('position')</code> throw an error because the FormBuilder cannot match <code>position</code> field on entity SportPlayer.</p>
  
  <p>How could I force matching between SportMeetupType and entity SportMeetup when SportMeetupType in an embed form?</p>