Permalink
Browse files

minor #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
  • Loading branch information...
weaverryan committed Mar 20, 2014
2 parents acf255d + 1e8b0d4 commit 868de1e9dfe4174f84e2d3d82e0aa0dafc559fb0
@@ -116,7 +116,7 @@ the event listener might look like the following::
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$product = $event->getData();
$form = $event->getForm();
@@ -147,7 +147,8 @@ the event listener might look like the following::
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
}
public function onPreSetData(FormEvent $event){
public function onPreSetData(FormEvent $event)
{
// ...
}
}
@@ -253,7 +254,7 @@ Using an event listener, your form might look like this::
->add('subject', 'text')
->add('body', 'textarea')
;
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
// ... add a choice list of friends of the current application user
});
}
@@ -653,7 +654,31 @@ field according to the current selection in the ``sport`` field:
{# ... #}
{{ form_end(form) }}
.. include:: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.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 ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>
.. code-block:: html+php
@@ -664,7 +689,31 @@ field according to the current selection in the ``sport`` field:
<!-- ... -->
<?php echo $view['form']->end($form) ?>
.. include:: /cookbook/form/dynamic_form_modification_ajax_js.rst.inc
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.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 ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>
The major benefit of submitting the whole form to just extract the updated
``position`` field is that no additional server-side code is needed; all the
@@ -1,25 +0,0 @@
<script>
var $sport = $('#meetup_sport');
// When sport gets selected ...
$sport.change(function(){
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$sport.attr('name')] = $sport.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 ...
$('#meetup_position').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#meetup_position')
);
// Position field now displays the appropriate positions.
}
});
});
</script>

0 comments on commit 868de1e

Please sign in to comment.