I have two Select box one for the Countries and the second for the cities and the second one depends from the selected choice of the first one.

I the code is as the example **Dynamic Generation for Submitted Forms** in the documentation **<http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data>**

All things work fine when adding a new post , but when trying do update a post, the **Ajax query** to display cities doesn't work .

This is the controller

```
// newAction
/**
* @ParamConverter("agence", options={"mapping": {"agence_slug":"slug"}})
*/
public function newAction(Agence $agence, Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $travel = new Travel();
    $form = $this->createForm(new TravelType($agence), $travel);

    if ($request->getMethod() == 'POST')
    {
        //....
    }

    return $this->render('AppBundle:Dashboard/Travel:new.html.twig',
    array(
        'form'   => $form->createView() ,
        'agence' => $agence,
        ));
}

//editAction
/**
* @ParamConverter("agence", options={"mapping": {"agence_slug":"slug"}})
* @ParamConverter("travel", options={"mapping": {"travel_id":"id"}})
*/
public function editAction(Travel $travel, Agence $agence, Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $form = $this->createForm(new TravelEditType($agence), $travel);

    if ($request->getMethod() == 'POST'){
        //....
    }

    return $this->render('AppBundle:Dashboard/Travel:edit.html.twig',
    array(
        'form' => $form->createView() ,
        'travel' => $travel,
        'agence' => $agence,
        ));
}

```

**travelType** and ti works good

```
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

//........
use Symfony\Component\Form\FormInterface;
use AppBundle\Entity\Country;

class TravelType extends AbstractType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
  //....
  $formModifier = function (FormInterface $form, Country $country = null) {
        $cities = null === $country ? array() : $country->getCities();

        $form->add('destination', 'entity', array(
            'class'       => 'AppBundle:CityWorld',
            'choices'     => $cities,
            'multiple' => false,
            'expanded' => false,
            'property'    => 'name',
            'label' => 'Destination',
            'attr' => array('class' => 'col-xs-10 col-sm-10', 'placeholder' => 'Destination'),
        ));
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
            // this would be your entity, i.e. SportMeetup
            $data = $event->getData();

            $formModifier($event->getForm(), $data->getCountry());
        }
    );

    $builder->get('country')->addEventListener(
        FormEvents::POST_SUBMIT,
        function (FormEvent $event) use ($formModifier) {
            // It's important here to fetch $event->getForm()->getData(), as
            // $event->getData() will get you the client data (that is, the ID)
            $country = $event->getForm()->getData();

            // since we've added the listener to the child, we'll have to pass on
            // the parent to the callback functions!
            $formModifier($event->getForm()->getParent(), $country);
        }
    );

    $builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
        $event->stopPropagation();
    }, 90000000000000); // Always set a higher priority than ValidationListener

}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Travel'
    ));
}

public function getName()
{
    return null;
}
}

```

This is **TravelEditType**

```
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
//........
use Symfony\Component\Form\FormInterface;
use AppBundle\Entity\Country;

class TravelEditType extends TravelType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
    parent::buildForm($builder, $options) ;

 }

 public function getName()
 {
    return null;
 }
}

```

This is the form and javascript code

```
<form method="post" action="" class="form-horizontal" role="form" {{ form_enctype(form) }} >
//.............
</form>

<script type="text/javascript">
var $county = $('#country');

$county.change(function () {
    // ... retrieve the corresponding form.
    var $form = $(this).closest('form');

    var data = {};
    data[$county.attr('name')] = $county.val();
    // Submit data via AJAX to the form's action path.
    $.ajax({
        url: $form.attr('action'),
        type: $form.attr('method'),
        data: data,
        success: function (html) {
            // Replace current position field ...
            $('#city').replaceWith(
                    // ... with the returned one from the AJAX response.
                    $(html).find('#city')
            );
            // Position field now displays the appropriate positions.
        }
    });
});

```