title: Setting data with an event listener and the FormEvent::PRE_SET_DATA Event in Symfony2
body: <p>I'm attempting to modify some data in a form based on other data that is in the form. From <a href="http://symfony.com/doc/current/components/form/form_events.html" rel="nofollow">the docs</a>:</p>
  
  <blockquote>
    <p>The FormEvents::PRE_SET_DATA event is dispatched at the beginning of the Form::setData() method. It can be used to:</p>
    
    <ul>
    <li><strong>Modify the data given during pre-population</strong></li>
    <li>Modify a form depending on the pre-populated data (adding or removing fields dynamically).</li>
    </ul>
  </blockquote>
  
  <p>This bolded line made me believe it should be possible to do something like the following in my <code>buildForm</code> function:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;addEventListener(\Symfony\Component\Form\FormEvents::PRE_SET_DATA,
          function(\Symfony\Component\Form\FormEvent $event) use ($options) {
              if ($options['default_to_nickname']) {
                  $securityContext = $this-&gt;container-&gt;get('security.context');
                  $nickname = null;
                  if ($securityContext-&gt;isGranted('IS_AUTHENTICATED_FULLY')) {
                      $user = $securityContext-&gt;getToken()-&gt;getUser();
                      $nickname = $user-&gt;getNickName();
                      if (isset($nickname))
                      {
                          $options['data'] = $nickname;
                          //$event-&gt;setData($nickname); //this doesn't work either
                      }
                  }
              }
          }
      );
  }
  </code></pre>
  
  <p>However, neither <code>options['data'] = $nickname;</code> nor <code>$event-&gt;setData($nickname);</code> result in the form being populated with the correct data. I can see that the data is being set in the FormEvent object when I call <code>$event-&gt;setData($nickname);</code>, but when the form is actually rendered, the data was not set. Am I expecting the wrong thing? I've looked into if something else might be modifying it after this, and there isn't. Any help would be very welcome!</p>