title: symfony2: presetdata event not working when form is a service
body: <p>With symfony I am trying to listen to the preset data event on a form declared as a service.</p>
  
  <p>I have a parent form in which I call ->add('unit', 'fmu_unit')</p>
  
  <p>EDIT : I add here a full simple example. It's not working as expected.</p>
  
  <p>My controller:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use AppBundle\Entity\MarketPlace\Product;
  use AppBundle\Form\UnitTestType;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  
  class DefaultController extends Controller
  {
      /**
       * @Route("/", name="home")
       * @Method({"POST", "GET"})
       * @Template(":Default:index.html.twig")
       */
      public function indexAction()
      {
          $formManager = $this-&gt;get('form_manager');
  
          $product = new Product();
          $unit = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:FoodAnalytics\Unit')-&gt;findOneByName('g');
          $product-&gt;setUnit($unit);
  
          $form = $formManager-&gt;createForm(new UnitTestType(), $product ,'POST', 'home');
  
          return array('form' =&gt; $form-&gt;createView());
      }
  }
  </code></pre>
  
  <p>My first Type (UnitTest):</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class UnitTestType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('unit', 'fmu_unit')
          ;
      }
  
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
                  'data_class' =&gt; 'AppBundle\Entity\MarketPlace\Product'
              ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'unit_test';
      }
  
  }
  </code></pre>
  
  <p>My second type (the service one in which I try to use Form Events):</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class UnitType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
                  $product = $event-&gt;getData();
                  $form = $event-&gt;getForm();
  
                  //This is not working
                  $form-&gt;add('unit','entity', array(
                          'class' =&gt; 'AppBundle:FoodAnalytics\Unit'
                      ));
              });
  
          //This is working
  //        $builder-&gt;add('unit','entity', array(
  //                'class' =&gt; 'AppBundle:FoodAnalytics\Unit'
  //            ));
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
                  'inherit_data' =&gt; true,
              ));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'fmu_unit';
      }
  
  }
  </code></pre>
  
  <p>The declaration as a service:</p>
  
  <pre><code>services:
      unit.type:
          class: %unit.type.class%
          tags:
              - { name: form.type, alias: fmu_unit }
  </code></pre>
  
  <p>The view :     {{ form(form) }}
  Nothing shows up but the label when I use the form Events. It works fine if I don't use it.</p>