title: Extending EntityType to allow extra choices set with AJAX calls
body: <p>I try to create a Symfony Custom type <strong>extending</strong> the core "<strong>entity</strong>" type.</p>
  
  <p>But I want to use it with <strong>Select2 version 4.0.0</strong> (ajax now works with "select" html element and not with hidden "input" like before).</p>
  
  <ul>
  <li>This type should create an empty select instead of the full list of entities by the extended "entity" type.</li>
  </ul>
  
  <p>This works by setting the option (see configureOption): </p>
  
  <pre><code>'choices'=&gt;array()
  </code></pre>
  
  <ul>
  <li>By editing the object attached to the form it should populate the select with the current data of the object. I solved this problem but just for the view with the following buildView method ...</li>
  </ul>
  
  <p>Select2 recognize the content of the html "select", and does its work with ajax.
  But when the form is posted back, Symfony doesn't recognize the selected choices, (because there were not allowed ?)</p>
  
  <pre><code>Symfony\Component\Form\Exception\TransformationFailedException
  
      Unable to reverse value for property path "user": The choice "28" does not exist or is not unique
  </code></pre>
  
  <p>I tried several methods using EventListeners or Subscribers but I can't find a working configuration.</p>
  
  <p>With Select2 3.5.* I solved the problem with form events and overriding the hidden formtype, but here extending the entitytype is much more difficult.</p>
  
  <p>How can I build my type to let it manage the reverse transformation of my entites ?</p>
  
  <p><strong>Custom type :</strong></p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormView;
  use Symfony\Component\Form\FormInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  use Symfony\Component\Form\ChoiceList\View\ChoiceView;
  
  class AjaxEntityType extends AbstractType
  {
      protected $router;
  
      public function __construct($router)
      {
          $this-&gt;router = $router;
      }
  
     /**
      * {@inheritdoc}
      */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {   
          $builder-&gt;setAttribute('attr',array_merge($options['attr'],array('class'=&gt;'select2','data-ajax--url'=&gt;$this-&gt;router-&gt;generate($options['route']))));
      }
  
      /**
      * {@inheritdoc}
      */
      public function buildView(FormView $view, FormInterface $form, array $options)
      {
          $view-&gt;vars['attr'] = $form-&gt;getConfig()-&gt;getAttribute('attr');
          $choices = array();
          $data=$form-&gt;getData();
          if($data instanceOf \Doctrine\ORM\PersistentCollection){$data = $data-&gt;toArray();}
          $values='';
          if($data != null){
              if(is_array($data)){
                  foreach($data as $entity){
                      $choices[] = new ChoiceView($entity-&gt;getAjaxName(),$entity-&gt;getId(),$entity,array('selected'=&gt;true));
                  }
              }
              else{
                  $choices[] = new ChoiceView($data-&gt;getAjaxName(),$data-&gt;getId(),$data,array('selected'=&gt;true));
              }
          }
  
          $view-&gt;vars['choices']=$choices;
      }
  
     /**
      * {@inheritdoc}
      */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setRequired(array('route'));
          $resolver-&gt;setDefaults(array('choices'=&gt;array(),'choices_as_value'=&gt;true));
      }
  
      public function getParent() {
          return 'entity';
      }
  
      public function getName() {
          return 'ajax_entity';
      }
  }
  </code></pre>
  
  <p><strong>Parent form</strong></p>
  
  <pre><code>&lt;?php
  namespace AppBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class AlarmsType extends AbstractType
  {
     /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name','text',array('required'=&gt;false))
              -&gt;add('user','ajax_entity',array("class"=&gt;"AppBundle:Users","route"=&gt;"ajax_users"))
              -&gt;add('submit','submit');
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array('data_class' =&gt; 'AppBundle\Entity\Alarms','validation_groups'=&gt;array('Default','form_user')));
      }
  
      /**
       * @return string
       */
      public function getName()
      {
          return 'alarms';
      }
  }
  </code></pre>