title: How to fill the dropdown list dynamically in symfony? (select the cities of the region)
body: <p>I explain my problem:</p>
  
  <p>I have to create a form where the drop-down lists are filled according to our choices in the previous ones.</p>
  
  <p>I have two entities:</p>
  
  <p>A Region may have several Cities (ManyToOne) relationship.</p>
  
  <p>I followed the documentation from here <a href="http://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data" rel="nofollow noreferrer">How to Dynamically Modify Forms Using Form Events (Dynamic Generation for Submitted Forms)</a> .</p>
  
  <p>Here is the code for the entities:</p>
  
  <p>Region entity:</p>
  
  <pre><code>class Region
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="nom", type="string", length=255)
       */
      private $nom;
  
      /**
       * @var int
       *
       * @ORM\Column(name="numero", type="smallint")
       */
      private $numero;
  }
  </code></pre>
  
  <p>Ville Entity:</p>
  
  <pre><code>class Ville
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="nom", type="string", length=255)
       */
      private $nom;
  
      /**
       * @var int
       *
       * @ORM\Column(name="numero", type="smallint")
       */
      private $numero;
  
      /**
       * @var int
       *
       * @ORM\Column(name="codePostal", type="smallint")
       */
      private $codePostal;
  
      /**
       * @var Region
       *
       * @ORM\ManyToOne(targetEntity="GE\CandidatBundle\Entity\Region")
       */
      protected $region;
  }
  </code></pre>
  
  <p>my problem:</p>
  
  <p>I retrieve the list of regions with the query_builder option but <strong>how do I retrieve the list of cities according to the choice of region</strong>.</p>
  
  <p>AnnonceType:</p>
  
  <pre><code> $builder
  -&gt;add('region', EntityType::class, [
                  'label' =&gt; 'Region *',
                  'label_attr' =&gt; [
                      "class" =&gt; "smaller lighter blue",
                      "style" =&gt; "font-size: 21px;",
                  ],
                  'class'         =&gt; 'GECandidatBundle:Region',
                  'choice_label'  =&gt; 'nom',
                  'multiple'      =&gt; false,
                  'query_builder' =&gt; function(RegionRepository $repository) {
                      return $repository-&gt;getListeRegion();
                  }
              ])
              -&gt;add('ville', TextType::class, [
                  'label' =&gt; 'Ville *',
                  'label_attr' =&gt; [
                      "class" =&gt; "smaller lighter blue",
                      "style" =&gt; "font-size: 21px;",
                  ],
              ]);
  $formModifier = function (FormInterface $form, Region $region = null) {
              $ville = null === $region ? array() : $region-&gt;getNom();
  
              $form-&gt;add('ville', EntityType::class, array(
                  'class' =&gt; 'GECandidatBundle:Ville',
                  'placeholder' =&gt; '',
                  'choices' =&gt; $ville,
  
                  'label' =&gt; 'Ville *',
                  'label_attr' =&gt; [
                      "class" =&gt; "smaller lighter blue",
                      "style" =&gt; "font-size: 21px;",
                  ],
                  'choice_label'  =&gt; 'nom',
                  'multiple'      =&gt; false,
                  /*'query_builder' =&gt; function(VilleRepository $repository) {
                      return $repository-&gt;getVilleByRegion();
                  }*/
              ));
          };
  
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function (FormEvent $event) use ($formModifier) {
                  $data = $event-&gt;getData();
                  $formModifier($event-&gt;getForm(), $data-&gt;getVille());
              }
          );
  
          $builder-&gt;get('region')-&gt;addEventListener(
              FormEvents::POST_SUBMIT,
              function (FormEvent $event) use ($formModifier) {
                  $region = $event-&gt;getForm()-&gt;getData();
                  $formModifier($event-&gt;getForm()-&gt;getParent(), $region);
              }
          );
  </code></pre>
  
  <p>new.html.twig:</p>
  
  <pre><code>    {{ form_start(form) }}
          {{ form_row(form.region) }}    {# &lt;select id="ge_candidatbundle_annonce_region" ... #}
          {{ form_row(form.ville) }} {# &lt;select id="ge_candidatbundle_annonce_ville" ... #}
          {# ... #}
      {{ form_end(form) }}
  &lt;script&gt;
          var $region = $('#ge_candidatbundle_annonce_region');
          $region.change(function() {
              var $form = $(this).closest('form');
              var data = {};
              data[$region.attr('nom')] = $region.val();
              $.ajax({
                  url : $form.attr('action'),
                  type: $form.attr('method'),
                  data : data,
                  success: function(html) {
                      $('#ge_candidatbundle_annonce_ville').replaceWith(
                          $(html).find('#ge_candidatbundle_annonce_ville')
                      );
                  }
              });
          });
      &lt;/script&gt;
  </code></pre>