Skip to content

Commit 868de1e

Browse files
committed
minor symfony#3698 Dynamic form modification cookbook: Fix inclusion of code (michaelperrin)
This PR was merged into the 2.3 branch. Discussion ---------- Dynamic form modification cookbook: Fix inclusion of code Unfortunately, reStructuredText doesn't support inclusion of code inside a code block, resulting to non-interpreted code (the `include` keyword is not displayed) as it happens on http://symfony.com/doc/2.3/cookbook/form/dynamic_form_modification.html . I tried in many ways to avoid this, but didn't find any nice way. I had to duplicate code instead. The PR includes a few minor fixes on code style as well. Commits ------- 1e8b0d4 Small fixes in code style 8093719 Dynamic form modification cookbook: Fix inclusion of code
2 parents acf255d + 1e8b0d4 commit 868de1e

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

cookbook/form/dynamic_form_modification.rst

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

669718
The 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

cookbook/form/dynamic_form_modification_ajax_js.rst.inc

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy