@@ -116,7 +116,7 @@ the event listener might look like the following::
116116 public function buildForm(FormBuilderInterface $builder, array $options)
117117 {
118118 // ...
119- $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
119+ $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
120120 $product = $event->getData();
121121 $form = $event->getForm();
122122
@@ -147,7 +147,8 @@ the event listener might look like the following::
147147 $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
148148 }
149149
150- public function onPreSetData(FormEvent $event){
150+ public function onPreSetData(FormEvent $event)
151+ {
151152 // ...
152153 }
153154 }
@@ -253,7 +254,7 @@ Using an event listener, your form might look like this::
253254 ->add('subject', 'text')
254255 ->add('body', 'textarea')
255256 ;
256- $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
257+ $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
257258 // ... add a choice list of friends of the current application user
258259 });
259260 }
@@ -653,7 +654,31 @@ field according to the current selection in the ``sport`` field:
653654 {# ... #}
654655 {{ form_end(form) }}
655656
656- .. include :: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
657+ <script>
658+ var $sport = $('#meetup_sport');
659+ // When sport gets selected ...
660+ $sport.change(function() {
661+ // ... retrieve the corresponding form.
662+ var $form = $(this).closest('form');
663+ // Simulate form data, but only include the selected sport value.
664+ var data = {};
665+ data[$sport.attr('name')] = $sport.val();
666+ // Submit data via AJAX to the form's action path.
667+ $.ajax({
668+ url : $form.attr('action'),
669+ type: $form.attr('method'),
670+ data : data,
671+ success: function(html) {
672+ // Replace current position field ...
673+ $('#meetup_position').replaceWith(
674+ // ... with the returned one from the AJAX response.
675+ $(html).find('#meetup_position')
676+ );
677+ // Position field now displays the appropriate positions.
678+ }
679+ });
680+ });
681+ </script>
657682
658683 .. code-block :: html+php
659684
@@ -664,7 +689,31 @@ field according to the current selection in the ``sport`` field:
664689 <!-- ... -->
665690 <?php echo $view['form']->end($form) ?>
666691
667- .. include :: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
692+ <script>
693+ var $sport = $('#meetup_sport');
694+ // When sport gets selected ...
695+ $sport.change(function() {
696+ // ... retrieve the corresponding form.
697+ var $form = $(this).closest('form');
698+ // Simulate form data, but only include the selected sport value.
699+ var data = {};
700+ data[$sport.attr('name')] = $sport.val();
701+ // Submit data via AJAX to the form's action path.
702+ $.ajax({
703+ url : $form.attr('action'),
704+ type: $form.attr('method'),
705+ data : data,
706+ success: function(html) {
707+ // Replace current position field ...
708+ $('#meetup_position').replaceWith(
709+ // ... with the returned one from the AJAX response.
710+ $(html).find('#meetup_position')
711+ );
712+ // Position field now displays the appropriate positions.
713+ }
714+ });
715+ });
716+ </script>
668717
669718The major benefit of submitting the whole form to just extract the updated
670719``position `` field is that no additional server-side code is needed; all the
0 commit comments