title: Symfony2 form events and model transformers
body: <p>I'm getting tied in knots trying to wrestle with Symfony2's form builders, events and transformers... hopefully somebody here is more experienced and can help out!</p>
  
  <p>I have a form field (select drop-down) which contains some values (a shortlist) which maps to an Entity. One of these options is "other". Assume there's no AJAX for now and when a user submits the form I want to detect if they chose 'other' (or any other option not in the shortlist). If they chose one of these options then the full list of options should be shown, otherwise just show the shortlist. Should be easy, right? ;)</p>
  
  <p>So, I have my Form Type and it displays the basic shortlist just fine. The code looks something like this:</p>
  
  <pre><code>namespace Company\ProjectBundle\Form\Type;
  
  use ...
  
  class FancyFormType extends AbstractType {
      private $fooRepo;
  
      public function __construct(EntityManager $em, FooRepository $fooRepo)
      {
          $this-&gt;fooRepo = $fooRepo;
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options) {
          /** @var Bar $bar */
          $bar = $builder-&gt;getData();
          $fooTransformer = new FooToStringTransformer($options['em']);
  
          $builder
              -&gt;add($builder
                  -&gt;create('linkedFoo', 'choice', array(
                      'choices' =&gt; $this-&gt;fooRepo-&gt;getListAsArray(
                          $bar-&gt;getLinkedfoo()-&gt;getId()
                      ),
                  ))
                  -&gt;addModelTransformer($fooTransformer)
              )
          ;
  
          // ...
  
      }
  
      // ...
  }
  </code></pre>
  
  <p>Now, I want to check the value submitted so I use a Form Event Listener as follows. </p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options) {
      // ... This code comes just after the snippet shown above
  
      $builder-&gt;addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
          /** @var EntityManager $em */
          $em = $event-&gt;getForm()-&gt;getConfig()-&gt;getOption('em');
  
          $data = $event-&gt;getData();
          if (empty($data['linkedFoo'])) return;
          $selectedFoo = $data['linkedfoo'];
  
          $event-&gt;getForm()-&gt;add('linkedFoo', 'choice', array(
              'choices' =&gt; $em
                  -&gt;getRepository('CompanyProjectBundle:FooShortlist')
                  -&gt;getListAsArray($selectedFoo)
              ,
          ));
          //@todo - needs transformer?
      });
  }
  </code></pre>
  
  <p>However, it fails with an error message like this:</p>
  
  <pre><code>Notice: Object of class Proxies\__CG__\Company\ProjectBundle\Entity\Foo could not be converted to int in \path\to\project\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php line 458 
  </code></pre>
  
  <p>I presume this error is because when the <code>linkedFoo</code> was over-written it removed the <code>modelTransformer</code>? I tried various ways of accessing a builder in the event's closure but this didn't seem to work (the return values were unexpected). Is there some other method I should be using in the event other than <code>$event-&gt;getForm()-&gt;add()</code>? Or is there a more fundamental problem with my approach here?</p>
  
  <p>Basically I don't want to mess with the <code>linkedFoo</code> field's config/transformers/labels <em>except</em> to change the choices available... is there some other way to do that? E.g. something like <code>$form-&gt;getField()-&gt;updateChoices()</code>?</p>
  
  <p>Thanks in advance for any help you can offer!</p>
  
  <p>C</p>
  
  <p>P.S. is there any better documentation or discussion of the forms, events, etc than on the Symfony website? E.g. what's the difference between PRE_SET_DATA, PRE_SUBMIT, SUBMIT, etc? When are they fired? What should they be used for? How does inheritance work with custom form fields? What is a Form and a Builder, how do they interact and when should you deal with each? How, when and why should you use the FormFactory you can access through <code>$form-&gt;getConfig()-&gt;getFormFactory()</code>? Etc..</p>
  
  <hr>
  
  <p>Edit: In response to Florian's suggestion here's some more info about things that were tried but do not work:</p>
  
  <p>If you try to get the FormBuilder within the event like this:</p>
  
  <pre><code>/** @var FormBuilder $builder */
  $builder = $event-&gt;getForm()-&gt;get('linkedFoo')-&gt;getConfig();
  
  $event-&gt;getForm()-&gt;add($builder
      -&gt;create('linkedFoo', 'choice', array(
          'choices' =&gt; $newChoices,
          'label'   =&gt;'label',
      ))
      -&gt;addModelTransformer(new FooToStringTransformer($em))
  );
  </code></pre>
  
  <p>Then you get the error:</p>
  
  <pre><code>FormBuilder methods cannot be accessed anymore once the builder is turned
  into a FormConfigInterface instance.
  </code></pre>
  
  <p>So then you try something like Florian suggested, i.e.</p>
  
  <pre><code>$event-&gt;getForm()-&gt;add('linkedFoo', 'choice', array(
      'choices' =&gt; $newChoices,
  ));
  $event-&gt;getForm()-&gt;get('linkedFoo')-&gt;getConfig()-&gt;addModelTransformer(new FooToStringTransformer($em));
  </code></pre>
  
  <p>...but you get this error instead:</p>
  
  <pre><code>Notice: Object of class Proxies\__CG__\Company\ProjectBundle\Entity\Foo could not be converted to int 
  in C:\path\to\vendor\symfony\symfony\src\Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList.php line 458
  </code></pre>
  
  <p>Which seems to suggest that the second line (which adds the ModelTransformer) is never called because the <code>-&gt;add()</code> call is failing before you can get there.</p>