Skip to content

Commit

Permalink
minor #3698 Dynamic form modification cookbook: Fix inclusion of code…
Browse files Browse the repository at this point in the history
… (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 868de1e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 30 deletions.
59 changes: 54 additions & 5 deletions cookbook/form/dynamic_form_modification.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
{
// ...
}
}
Expand Down Expand Up @@ -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
});
}
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
25 changes: 0 additions & 25 deletions cookbook/form/dynamic_form_modification_ajax_js.rst.inc

This file was deleted.

0 comments on commit 868de1e

Please sign in to comment.