title: Symfony2 Form - Embed forms and event listeners
body: <p>I have a form "AddressType" with an event listener that works as expected when instantiated alone. Here is the code.</p>
  
  <p>
  
  <pre><code>namespace Nc\ClientsBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  use Nc\ClientsBundle\Form\EventListener\AddCityFieldSubscriber;
  
  class AddressType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder
              -&gt;add('street')
              -&gt;add('number', 'text')
              -&gt;add('complement')
              -&gt;add('district')
              -&gt;add('state', 'entity', array(
                  'class' =&gt; 'ClientsBundle:State', 
                  'property' =&gt; 'name',
              ));
          $subscriber = new AddCityFieldSubscriber($builder-&gt;getFormFactory());
          $builder-&gt;addEventSubscriber($subscriber);
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'data_class' =&gt; 'Nc\ClientsBundle\Entity\Address',
          );
      }
  
      public function getName()
      {
          return 'addresstype';
      }
  }
  
  
  &lt;?php
  
  namespace Nc\ClientsBundle\Form\EventListener;
  
  use Symfony\Component\Form\Event\DataEvent;
  use Symfony\Component\Form\FormFactoryInterface;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use Symfony\Component\Form\FormEvents;
  use Doctrine\ORM\EntityRepository;
  
  class AddCityFieldSubscriber implements EventSubscriberInterface
  {
      private $factory;
  
      public function __construct(FormFactoryInterface $factory)
      {
          $this-&gt;factory = $factory;
      }
  
      public static function getSubscribedEvents()
      {
          // Tells the dispatcher that we want to listen on the form.pre_set_data
          // event and that the preSetData method should be called.
          return array(FormEvents::PRE_SET_DATA  =&gt; 'preSetData');
      }
  
      public function preSetData(DataEvent $event)
      {
          $data = $event-&gt;getData();
          $form = $event-&gt;getForm();
  
          // During form creation setData() is called with null as an argument
          // by the FormBuilder constructor. We're only concerned with when
          // setData is called with an actual Entity object in it (whether new,
          // or fetched with Doctrine). This if statement let's us skip right
          // over the null condition.
          if (null === $data) {
              return;
          }
  
          if (!$data-&gt;getCity()) {
  
              $form-&gt;add($this-&gt;factory-&gt;createNamed('city_selector', 'city', null, array(
                  'choices'   =&gt; array('' =&gt; '--  Selecione um estado  --'),
                  'required'  =&gt; true,
                  'expanded'  =&gt; false,
                  'multiple'  =&gt; false,
              )));
  
          } else {
  
              $city = $data-&gt;getCity();
              $form-&gt;add($this-&gt;factory-&gt;createNamed('city_selector', 'city', null, array(
                  'choices'   =&gt; array($city-&gt;getId() =&gt; $city-&gt;getName()),
                  'required'  =&gt; true,
                  'expanded'  =&gt; false,
                  'multiple'  =&gt; false,
              )));
          }
  
      }
  
  }
  
  &lt;?php
  
  namespace Nc\ClientsBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  use Doctrine\Common\Persistence\ObjectManager;
  use Nc\ClientsBundle\Form\DataTransformer\CityToIdTransformer;
  
  class CitySelectorType extends AbstractType
  {
      /**
       * @var ObjectManager
       */
      private $om;
  
      /**
       * @param ObjectManager $om
       */
      public function __construct(ObjectManager $om)
      {
          $this-&gt;om = $om;
      }
  
      public function buildForm(FormBuilder $builder, array $options)
      {
          $transformer = new CityToIdTransformer($this-&gt;om);
          $builder-&gt;prependNormTransformer($transformer);
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'invalid_message' =&gt; 'Selecione um estado e uma cidade.',
          );
      }
  
      public function getParent(array $options)
      {
          return 'choice';
      }
  
      public function getName()
      {
          return 'city_selector';
      }
  }
  
  &lt;?php
  
  namespace Nc\ClientsBundle\Form\DataTransformer;
  
  use Symfony\Component\Form\DataTransformerInterface;
  use Symfony\Component\Form\Exception\TransformationFailedException;
  use Doctrine\Common\Persistence\ObjectManager;
  use Nc\ClientsBundle\Entity\City;
  
  class CityToIdTransformer implements DataTransformerInterface
  {
      /**
       * @var ObjectManager
       */
      private $om;
  
      /**
       * @param ObjectManager $om
       */
      public function __construct(ObjectManager $om)
      {
          $this-&gt;om = $om;
      }
  
      /**
       * Transforms an object (city) to a integer (id).
       *
       * @param  City|null $city
       * @return string
       */
      public function transform($city)
      {
          if ($city === null) {
              return '';
          }
  
          return $city-&gt;getId();
  
      }
  
      /**
       * Transforms a string (id) to an object (city).
       *
       * @param  string $id
       * @return City|null
       * @throws TransformationFailedException if object (city) is not found.
       */
      public function reverseTransform($id)
      {
          if (!$id) {
              return null;
          }
  
          $city = $this-&gt;om
              -&gt;getRepository('ClientsBundle:City')
              -&gt;findOneBy(array('id' =&gt; $id))
          ;
  
          if (null === $city) {
              throw new TransformationFailedException(sprintf(
                  'An city with id "%s" does not exist!',
                  $id
              ));
          }
  
          return $city;
      }
  }
  </code></pre>
  
  <p>What happens is that when I try to embed this form into the "ClientType" form, the "city" field is not rendered.</p>
  
  <p>
  
  <pre><code>namespace Nc\ClientsBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  
  class ClientType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder-&gt;add('name');
          $builder-&gt;add('type', 'choice', array(
              'choices'   =&gt; array('1' =&gt; 'Comum', '2' =&gt; 'Parceiro'),
              'expanded'  =&gt; true,
              'multiple'  =&gt; false,
              'required'  =&gt; true,
          ));
          $builder-&gt;add('contactInfo', new ContactInfoType(), array('label' =&gt; ' '));
          $builder-&gt;add('address', new AddressType(), array('label' =&gt; ' '));
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'data_class' =&gt; 'Nc\ClientsBundle\Entity\Client',
          );
      }
  
      public function getName()
      {
          return 'clienttype';
      }
  }
  </code></pre>