title: Accessing a form field from a subscriber (of a form event) in Symfony2
body: <p>I'm following the tutorial <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html" rel="nofollow">How to Dynamically Generate Forms Using Form Events</a>. I'm stuck on the creation of <code>AddNameFieldSubscriber</code>:</p>
  
  <pre><code>$subscriber = new AddNameFieldSubscriber($builder-&gt;getFormFactory());
  </code></pre>
  
  <p>My question is simple: how <code>FormFactory</code> can access and modify an arbitrary form field previously created by the <code>$builder</code>? And why we are passing the <code>FormFactory</code> instead of the <code>$builder</code> itself?</p>
  
  <p>Assuming we have just two fields ("name" and "price") in the <strong>builder</strong>:</p>
  
  <pre><code>class ProductType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
          $subscriber = new AddProductTypeSubscriber($builder-&gt;getFormFactory());
          $builder-&gt;addEventSubscriber($subscriber);
  
          $builder-&gt;add('name');
          $builder-&gt;add('price');
      }
  
      public function getName() { return 'product'; }
  }
  </code></pre>
  
  <p>I'd like to set <code>required = false</code> (just an example) in the <strong>subscriber</strong>:</p>
  
  <pre><code>class ProductTypeSubscriber implements EventSubscriberInterface
  {
      private $factory;
  
      public function __construct(FormFactoryInterface $factory)
      {
          $this-&gt;factory = $factory;
      }
  
      public static function getSubscribedEvents()
      {
          return array(FormEvents::PRE_SET_DATA =&gt; 'preSetData');
      }
  
      public function preSetData(DataEvent $event)
      {
          $data = $event-&gt;getData();
          $form = $event-&gt;getForm();
  
          if (null === $data) return;
  
          // Access "name" field and set require = false
      }
  }
  </code></pre>