Skip to content

Commit adf8989

Browse files
committed
[#2092] Minor tweaks to new form request handlers and corresponding twig functions
1 parent 7f89c45 commit adf8989

File tree

8 files changed

+120
-53
lines changed

8 files changed

+120
-53
lines changed

book/forms.rst

+48-40
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ helper functions:
162162
change the request method and the target URL of the form.
163163

164164
That's it! By printing ``form(form)``, each field in the form is rendered, along
165-
with a label and error message (if there is one). As easy as this is, it's not
166-
very flexible (yet). Usually, you'll want to render each form field individually
167-
so you can control how the form looks. You'll learn how to do that in the
168-
":ref:`form-rendering-template`" section.
165+
with a label and error message (if there is one). The ``form`` function also
166+
surrounds everything in the necessary HTML ``form`` tag. As easy as this is,
167+
it's not very flexible (yet). Usually, you'll want to render each form field
168+
individually so you can control how the form looks. You'll learn how to do
169+
that in the ":ref:`form-rendering-template`" section.
169170

170171
Before moving on, notice how the rendered ``task`` input field has the value
171172
of the ``task`` property from the ``$task`` object (i.e. "Write a blog post").
@@ -188,6 +189,8 @@ it into a format that's suitable for being rendered in an HTML form.
188189
.. index::
189190
single: Forms; Handling form submission
190191

192+
.. _book-form-handling-form-submissions:
193+
191194
Handling Form Submissions
192195
~~~~~~~~~~~~~~~~~~~~~~~~~
193196

@@ -222,8 +225,8 @@ controller::
222225

223226
.. versionadded:: 2.3
224227
The :method:`Symfony\Component\Form\FormInterface::handleRequest` method was
225-
added in Symfony 2.3. Before you had to do some manual work to achieve the
226-
same result.
228+
added in Symfony 2.3. Previously, the now-deprecated ``bind`` function
229+
was used. For details on that method, see :doc:`/cookbook/form/deprecated_bind`.
227230

228231
This controller follows a common pattern for handling forms, and has three
229232
possible paths:
@@ -669,39 +672,46 @@ used the ``form_row`` helper:
669672

670673
.. code-block:: html+jinja
671674

672-
{{ form_errors(form) }}
675+
{{ form_start(form) }}
676+
{{ form_errors(form) }}
673677

674-
<div>
675-
{{ form_label(form.task) }}
676-
{{ form_errors(form.task) }}
677-
{{ form_widget(form.task) }}
678-
</div>
678+
<div>
679+
{{ form_label(form.task) }}
680+
{{ form_errors(form.task) }}
681+
{{ form_widget(form.task) }}
682+
</div>
679683

680-
<div>
681-
{{ form_label(form.dueDate) }}
682-
{{ form_errors(form.dueDate) }}
683-
{{ form_widget(form.dueDate) }}
684-
</div>
684+
<div>
685+
{{ form_label(form.dueDate) }}
686+
{{ form_errors(form.dueDate) }}
687+
{{ form_widget(form.dueDate) }}
688+
</div>
685689

686-
{{ form_rest(form) }}
690+
<input type="submit" />
691+
692+
{{ form_end(form) }}
687693

688694
.. code-block:: html+php
689695

690-
<?php echo $view['form']->errors($form) ?>
696+
<?php echo $view['form']->start($form) ?>
697+
698+
<?php echo $view['form']->errors($form) ?>
691699

692-
<div>
693-
<?php echo $view['form']->label($form['task']) ?>
694-
<?php echo $view['form']->errors($form['task']) ?>
695-
<?php echo $view['form']->widget($form['task']) ?>
696-
</div>
700+
<div>
701+
<?php echo $view['form']->label($form['task']) ?>
702+
<?php echo $view['form']->errors($form['task']) ?>
703+
<?php echo $view['form']->widget($form['task']) ?>
704+
</div>
697705

698-
<div>
699-
<?php echo $view['form']->label($form['dueDate']) ?>
700-
<?php echo $view['form']->errors($form['dueDate']) ?>
701-
<?php echo $view['form']->widget($form['dueDate']) ?>
702-
</div>
706+
<div>
707+
<?php echo $view['form']->label($form['dueDate']) ?>
708+
<?php echo $view['form']->errors($form['dueDate']) ?>
709+
<?php echo $view['form']->widget($form['dueDate']) ?>
710+
</div>
703711

704-
<?php echo $view['form']->rest($form) ?>
712+
<input type="submit" />
713+
714+
<?php echo $view['form']->end($form) ?>
705715

706716
If the auto-generated label for a field isn't quite right, you can explicitly
707717
specify it:
@@ -777,8 +787,8 @@ that can be used with each.
777787
Changing the Action and Method of a Form
778788
----------------------------------------
779789

780-
So far, we have used the ``form_start()`` helper to render the form's start tag
781-
and assumed that each form is submitted to the same URL in a POST request.
790+
So far, the ``form_start()`` helper has been used to render the form's start
791+
tag and we assumed that each form is submitted to the same URL in a POST request.
782792
Sometimes you want to change these parameters. You can do so in a few different
783793
ways. If you build your form in the controller, you can use ``setAction()`` and
784794
``setMethod()``::
@@ -795,16 +805,16 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
795805
This example assumes that you've created a route called ``target_route``
796806
that points to the controller that processes the form.
797807

798-
In :ref:`book-form-creating-form-classes` you will learn how to outsource the
799-
form building code into separate classes. When using such a form class in the
800-
controller, you can pass the action and method as form options::
808+
In :ref:`book-form-creating-form-classes` you will learn how to move the
809+
form building code into separate classes. When using an external form class
810+
in the controller, you can pass the action and method as form options::
801811

802812
$form = $this->createForm(new TaskType(), $task, array(
803813
'action' => $this->generateUrl('target_route'),
804814
'method' => 'GET',
805815
));
806816

807-
At last, you can override the action and method in the template by passing them
817+
Finally, you can override the action and method in the template by passing them
808818
to the ``form()`` or the ``form_start()`` helper:
809819

810820
.. configuration-block::
@@ -1100,7 +1110,6 @@ as the original ``Task`` fields:
11001110
{{ form_row(form.category.name) }}
11011111
</div>
11021112

1103-
{{ form_rest(form) }}
11041113
{# ... #}
11051114

11061115
.. code-block:: html+php
@@ -1112,7 +1121,6 @@ as the original ``Task`` fields:
11121121
<?php echo $view['form']->row($form['category']['name']) ?>
11131122
</div>
11141123

1115-
<?php echo $view['form']->rest($form) ?>
11161124
<!-- ... -->
11171125

11181126
When the user submits the form, the submitted data for the ``Category`` fields
@@ -1480,7 +1488,7 @@ ensures that the user - not some other entity - is submitting the given data.
14801488
Symfony automatically validates the presence and accuracy of this token.
14811489

14821490
The ``_token`` field is a hidden field and will be automatically rendered
1483-
if you include the ``form_rest()`` function in your template, which ensures
1491+
if you include the ``form_end()`` function in your template, which ensures
14841492
that all un-rendered fields are output.
14851493

14861494
The CSRF token can be customized on a form-by-form basis. For example::
@@ -1543,7 +1551,7 @@ an array of the submitted data. This is actually really easy::
15431551

15441552
$form->handleRequest($request);
15451553

1546-
if ($form->isBound()) {
1554+
if ($form->isValid()) {
15471555
// data is an array with "name", "email", and "message" keys
15481556
$data = $form->getData();
15491557
}

cookbook/doctrine/file_uploads.rst

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ The following controller shows you how to handle the entire process::
222222
// ...
223223
use Acme\DemoBundle\Entity\Document;
224224
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
225+
use Symfony\Component\HttpFoundation\Request;
225226
// ...
226227

227228
/**

cookbook/doctrine/registration_form.rst

+4-3
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ controller for displaying the registration form::
234234
{
235235
$registration = new Registration();
236236
$form = $this->createForm(new RegistrationType(), $registration, array(
237-
'action' => $this->generateUrl('create'),
237+
'action' => $this->generateUrl('account_create'),
238238
));
239239

240240
return $this->render(
@@ -251,8 +251,9 @@ and its template:
251251
{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
252252
{{ form(form) }}
253253

254-
Finally, create the controller which handles the form submission. This performs
255-
the validation and saves the data into the database::
254+
Finally, create the controller (and corresponding ``account_create``) which
255+
handles the form submission. This performs the validation and saves the data
256+
into the database::
256257

257258
public function createAction(Request $request)
258259
{

cookbook/form/deprecated_bind.rst

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. index::
2+
single: Form; Form testing
3+
4+
How to use the (deprecated) bind Function to handle Form Submissions
5+
====================================================================
6+
7+
In Symfony 2.3, a new :method:`Symfony\Component\Form\FormInterface::handleRequest`
8+
method was added, which makes handling form submissions easier than ever::
9+
10+
use Symfony\Component\HttpFoundation\Request;
11+
// ...
12+
13+
public function newAction(Request $request)
14+
{
15+
$form = $this->createFormBuilder()
16+
// ...
17+
->getForm();
18+
19+
$form->handleRequest($request);
20+
21+
if ($form->isValid()) {
22+
// perform some action...
23+
24+
return $this->redirect($this->generateUrl('task_success'));
25+
}
26+
27+
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
28+
'form' => $form->createView(),
29+
));
30+
}
31+
32+
.. tip::
33+
34+
To see more about this method, read :ref:`book-form-handling-form-submissions`.
35+
36+
Prior to this, the :method:`Symfony\Component\Form\FormInterface::bind` method
37+
was used instead::
38+
39+
use Symfony\Component\HttpFoundation\Request;
40+
// ...
41+
42+
public function newAction(Request $request)
43+
{
44+
$form = $this->createFormBuilder()
45+
// ...
46+
->getForm();
47+
48+
if ($request->isMethod('POST')) {
49+
$form->bind($request);
50+
51+
if ($form->isValid()) {
52+
// perform some action...
53+
54+
return $this->redirect($this->generateUrl('task_success'));
55+
}
56+
}
57+
58+
return $this->render('AcmeTaskBundle:Default:new.html.twig', array(
59+
'form' => $form->createView(),
60+
));
61+
}
62+
63+
This still works, but is deprecated and will be removed in Symfony 3.0.

cookbook/form/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ Form
1313
use_virtuals_forms
1414
unit_testing
1515
use_empty_data
16+
deprecated_bind

cookbook/form/unit_testing.rst

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The simplest ``TypeTestCase`` implementation looks like the following::
5353
$object = new TestObject();
5454
$object->fromArray($formData);
5555

56+
// bind the data to the form directly
5657
$form->bind($formData);
5758

5859
$this->assertTrue($form->isSynchronized());

cookbook/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* :doc:`/cookbook/form/use_virtuals_forms`
8888
* :doc:`/cookbook/form/unit_testing`
8989
* :doc:`/cookbook/form/use_empty_data`
90+
* :doc:`/cookbook/form/deprecated_bind`
9091
* (validation) :doc:`/cookbook/validation/custom_constraint`
9192
* (doctrine) :doc:`/cookbook/doctrine/file_uploads`
9293

reference/forms/twig_reference.rst

+1-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Renders the HTML of a complete form.
3636
{# render the form and change the submission method #}
3737
{{ form(form, {'method': 'GET'}) }}
3838
39-
You will mostly use this helper for prototyping and if you use custom form
39+
You will mostly use this helper for prototyping or if you use custom form
4040
themes. If you need more flexibility in rendering the form, you should use
4141
the other helpers to render individual parts of the form instead:
4242

@@ -51,9 +51,6 @@ the other helpers to render individual parts of the form instead:
5151
<input type="submit" value="Submit me"/>
5252
{{ form_end(form) }}
5353
54-
See ":ref:`twig-reference-form-variables`" to learn more about the ``variables``
55-
argument.
56-
5754
.. _reference-forms-twig-start:
5855

5956
form_start(view, variables)
@@ -68,9 +65,6 @@ correct ``enctype`` property if the form contains upload fields.
6865
{# render the start tag and change the submission method #}
6966
{{ form_start(form, {'method': 'GET'}) }}
7067
71-
See ":ref:`twig-reference-form-variables`" to learn more about the ``variables``
72-
argument.
73-
7468
.. _reference-forms-twig-end:
7569

7670
form_end(view, variables)
@@ -90,9 +84,6 @@ false:
9084
{# don't render unrendered fields #}
9185
{{ form_end(form, {'render_rest': false}) }}
9286
93-
See ":ref:`twig-reference-form-variables`" to learn more about the ``variables``
94-
argument.
95-
9687
.. _reference-forms-twig-label:
9788

9889
form_label(view, label, variables)

0 commit comments

Comments
 (0)