title: Symfony2 Form Events Listener and Data Transformer Error Cannot instantiate interface Doctrine\....\ObjectManager
body: <p>I'm working on a Symfony 2.3 form which require that a field be only available on "create new" mode.</p>
  
  <p>The field is a link in a Many-to-One relationship, i've managed to change the dropdown list by using a jQuery AutoComplete for ease of use via a <code>FormEvents::PRE_SET_DATA</code> event ,however the <code>FormEvents::PRE_SUBMIT</code> which is to turn the ID supplied into an object require a Doctrine ObjectManager.
  This turn to be an Interface and throw the <strong>'Cannot instantiate interface Doctrine\Common\Persistence\ObjectManager'</strong> .<br>
  I've looked at the following similar questions and documentations (<a href="https://stackoverflow.com/questions/19382017/symfony2-form-events-and-model-transformers">Form Events</a> , <a href="https://stackoverflow.com/questions/11107821/symfony2-form-events-drop-down-lists">Form Events Dropdown</a>, <a href="http://symfony.com/doc/2.3/cookbook/form/data_transformers.html#harder-example-transforming-an-issue-number-into-an-issue-entity" rel="nofollow noreferrer">Doctrine Data Transformer</a>, <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html" rel="nofollow noreferrer">Dynamic Form Modification</a>) but being so close to the end(And  a newbie to both php and symfony) i wish no to re-write this piece.</p>
  
  <p>May you find below my Event Listener and Data Transformer classes:</p>
  
  <pre><code>namespace XXXXXXXBundle\Form\EventListener;
  
  use Doctrine\Common\Persistence\ObjectManager;
  use Symfony\Component\Form\FormEvents;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  use XXXXXXXXXXXXXBundle\Form\DataTransformer\rneToEcoleTransformer;
  
  class AddFieldSubscriber implements EventSubscriberInterface {
  
  private $manager;
  
  public static function getSubscribedEvents() {
      // Tells the dispatcher that you want to listen on the form.pre_set_data
      // event and that the preSetData method should be called.
      return array(\Symfony\Component\Form\FormEvents::PRE_SET_DATA =&gt; 'preSetData',
          \Symfony\Component\Form\FormEvents::PRE_SUBMIT =&gt; 'preSubmit');
  }
  
  public function preSetData(\Symfony\Component\Form\FormEvent $event) {
      $parents = $event-&gt;getData();
      $form = $event-&gt;getForm();
  
      //Verifie si le parent d'eleve est un nouveau 
      //ou s'il s'agit d' une mise à jour
      if (!$parents || null === $parents-&gt;getId()) {
          $form-&gt;add('ecole', 'text', array('invalid_message' =&gt; 'Ce RNE n\'est pas valide',
              'attr' =&gt; array('data-id' =&gt; 'ecoles',
                  'data-url' =&gt; 'parents_json_ecoles'
              )
                  )
          );
      }
  }
  
  public function preSubmit(\Symfony\Component\Form\FormEvent $event) {
      $this-&gt;manager = new \Doctrine\Common\Persistence\ObjectManager;
  
      $parents = $event-&gt;getData();
      if (empty($parents['ecole'])) {
          return;
      }
      $event-&gt;getForm()-&gt;get('ecole')-&gt;getConfig()-&gt;addModelTransformer(new rneToEcoleTransformer($this-&gt;manager));
  }}
  
  namespace XXXXXXXBundle\Form\DataTransformer;
  use Symfony\Component\Form\DataTransformerInterface;
  use Symfony\Component\Form\Exception\TransformationFailedException;
  
  class rneToEcoleTransformer implements DataTransformerInterface {
  
  private $manager;
  
  public function __construct(\Doctrine\Common\Persistence\ObjectManager $manager) {
      $this-&gt;manager = $manager;
  }
  
  
  public function transform($ecole) {
      if (null === $ecole) {
          return '';
      }
      return $ecole-&gt;getRne();
  }
  
  
  public function reverseTransform($rneNumber) {
      if (!$rneNumber) {
          return;
      }
  
      $ecole = $this-&gt;manager
              -&gt;getRepository('XXXXXXXBundle:Ecole')
              -&gt;find($rneNumber);
      if (null === $ecole) {
            throw new TransformationFailedException(sprintf(
                  'No matching data was found "%s" !', $rneNumber
          ));
      }
  
      return $ecole;
  }}
  </code></pre>
  
  <p>Here is the action used :</p>
  
  <pre><code>private function createCreateForm(Parents $entity) {
      $manager = $this-&gt;getDoctrine()-&gt;getManager();
      $form = $this-&gt;createForm(new ParentsType($manager), $entity, array(
          'action' =&gt; $this-&gt;generateUrl('parents_create'),
          'method' =&gt; 'POST',
      ));
  
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Add'));
  
      return $form;
  }
  </code></pre>
  
  <p>The Formtype :</p>
  
  <pre><code>class XXXXType extends AbstractType {
  
  private $manager;
  
  public function __construct(\Doctrine\Common\Persistence\ObjectManager $manager) {
      $this-&gt;manager = $manager;
  }
  
  /**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options) {
      $builder
              -&gt;add('nom', 'text', array('label' =&gt; 'Nom *',
                  'attr' =&gt; array('placeholder' =&gt; 'Nom ',
                      'maxlength' =&gt; 250,
                      'trim' =&gt; true)
                      )
              )
              -&gt;add('prenoms', 'text', array('label' =&gt; 'Prénom(s) *',
                  'attr' =&gt; array('placeholder' =&gt; 'Prénom(s)',
                      'maxlength' =&gt; 250,
                      'trim' =&gt; true)
                      )
              )
              -&gt;add('date_de_naiss', 'date', array('widget' =&gt; 'single_text', 'format' =&gt; 'dd-MM-yyyy',
                  'label' =&gt; 'Date de naissance ',
                  'required' =&gt; false, 'data' =&gt; null,
                  'attr' =&gt; array('name' =&gt; 'datepicker')
                      )
              )
              -&gt;add('telephone', 'text', array('label' =&gt; 'Telephone *',
                  'attr' =&gt; array('placeholder' =&gt; '123-456-7890 or 1234567890')
                      )
              )
              -&gt;add('email', 'email', array('attr' =&gt; array('placeholder' =&gt; 'xxxx@xxxx.com',),
                  'trim' =&gt; true,
                  'required' =&gt; false,
                  'data' =&gt; null
                      )
                    );
  
  
      $builder-&gt;addEventSubscriber(new AddFieldSubscriber($manager));
  }
  
  /**
   * @param OptionsResolverInterface $resolver
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver) {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'XXXXXXBundle\Entity\Parents'
      ));
  }
  
  /**
   * @return string
   */
  public function getName() {
      return 'XXXXXXXXXsbundle_parents';
  }
  
  }
  </code></pre>