title: Entity field in form with extra option
body: <p>Hope somebody helps me, I can't get around this.</p>
  <p>I'm working with Symfony 3.1.6.</p>
  <p>I have two entities, Regions and Cities, with a relation of one to many. I have a form with a select box that loads the regions and in function of which region is selected shows the cities of this region. That works great.</p>
  <p>I have added an option to the Cities combo box, via finishView(), to give the user the opportunity to, if his city is not listed, to select this option and with an input that appears offer the possibility to add a new city. This option value is 0.</p>
  <p>But when the form is summited an error is displayed with a &quot;This value is not valid.&quot; and I think this is because there isn't any city of any region with id = 0, the value of the option. Then what I need is to disable/eliminate/what ever this validation but I don't see how to do it.</p>
  <p>I have tried:</p>
  <pre class="lang-php prettyprint-override"><code>$builder-&gt;addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
      $event-&gt;stopPropagation();
  }, 900);
  </code></pre>
  <p>But it didn't work.</p>
  <p>Obviously I can go the ChoiceType instead of EntityType, eliminate the relationship between entities or other not so elegant ways to solve this. But I'm pretty sure there is a way, this is Symfony.</p>
  <p><strong>EDIT 1 : Adding LocationType</strong></p>
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  namespace AppBundle\Form;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormEvent;
  use Symfony\Component\Form\FormEvents;
  use Doctrine\ORM\EntityRepository;
  use Symfony\Component\Form\Extension\Core\Type\CountryType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\FormView;
  use Symfony\Component\Form\FormInterface;
  use Symfony\Component\Form\ChoiceList\View\ChoiceView;
  
  class LocationType extends AbstractType
  {
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      
          $region_options = array(
              'class' =&gt; 'AppBundle:Region',
              'choice_label' =&gt; 'name',
              'label' =&gt; 'Region',
              'translation_domain' =&gt; 'messages',
              'required' =&gt; true,
              'mapped' =&gt; false,
          );
  
          $city_options = array(
              'class' =&gt; 'AppBundle:City',
              'choice_label' =&gt; 'name',
              'label' =&gt; 'City',
              'translation_domain' =&gt; 'messages',
              'required' =&gt; true,
              'mapped' =&gt; false,
          );
  
          $alt_city_options = array(
              'required' =&gt; false,
              'mapped' =&gt; true,
              'label' =&gt; 'Alternative city',
              'translation_domain' =&gt; 'messages',
              'attr' =&gt; array(
                  'placeholder' =&gt; 'Enter your city name',
              ),
          );
  
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($region_options, $city_options, $alt_city_options) {
                  $form = $event-&gt;getForm();
                  $data = $event-&gt;getForm()-&gt;getParent()-&gt;getData();
  
                  $country = '332';
  
                  $region_options['query_builder'] = function (EntityRepository $er) use ($country) {
                          return $er-&gt;createQueryBuilder('r')
                              -&gt;where('r.country = :country')
                              -&gt;setParameter('country', $country)
                              -&gt;orderBy('r.name', 'ASC');
                      };
  
                      if (!empty($data-&gt;getRegion())) {
                          $region_options['data'] = $data-&gt;getRegion()-&gt;getId();
                      }
                      $form-&gt;add('region', EntityType::class, $region_options);
                  }
  
                  if ( !empty($data-&gt;getRegion()) ) {
                      $region = $data-&gt;getRegion();
                      $city_options['query_builder'] = function (EntityRepository $er) use ($country, $region) {
                          return $er-&gt;createQueryBuilder('c')
                              -&gt;where('c.country = :country')
                              -&gt;andWhere('c.region = :region')
                              -&gt;setParameter('country', $country)
                              -&gt;setParameter('region', $region)
                              -&gt;orderBy('c.name', 'ASC');
                      };
  
                      if (!empty($data-&gt;getCity())) {
                          $region_options['data'] = $data-&gt;getCity()-&gt;getId();
                      }
                      $form-&gt;add('city', EntityType::class, $city_options);
                  }
                  $form-&gt;add('alt_city', TextType::class, $alt_city_options);
              }
          );
  
          $builder-&gt;addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
              $event-&gt;stopPropagation();
          }, 900);
      }
  
      public function finishView(FormView $view, FormInterface $form, array $options)
      {
          $new_choice = new ChoiceView(array(), '0', 'not_in_the_list');
          $view-&gt;children['city']-&gt;vars['choices'][] = $new_choice;
      }
  }
  </code></pre>