@@ -162,10 +162,11 @@ helper functions:
162
162
change the request method and the target URL of the form.
163
163
164
164
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.
169
170
170
171
Before moving on, notice how the rendered ``task `` input field has the value
171
172
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.
188
189
.. index ::
189
190
single: Forms; Handling form submission
190
191
192
+ .. _book-form-handling-form-submissions :
193
+
191
194
Handling Form Submissions
192
195
~~~~~~~~~~~~~~~~~~~~~~~~~
193
196
@@ -222,8 +225,8 @@ controller::
222
225
223
226
.. versionadded :: 2.3
224
227
The :method: `Symfony\C omponent\F orm\F ormInterface::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 ` .
227
230
228
231
This controller follows a common pattern for handling forms, and has three
229
232
possible paths:
@@ -669,39 +672,46 @@ used the ``form_row`` helper:
669
672
670
673
.. code-block :: html+jinja
671
674
672
- {{ form_errors(form) }}
675
+ {{ form_start(form) }}
676
+ {{ form_errors(form) }}
673
677
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>
679
683
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>
685
689
686
- {{ form_rest(form) }}
690
+ <input type="submit" />
691
+
692
+ {{ form_end(form) }}
687
693
688
694
.. code-block :: html+php
689
695
690
- <?php echo $view['form']->errors($form) ?>
696
+ <?php echo $view['form']->start($form) ?>
697
+
698
+ <?php echo $view['form']->errors($form) ?>
691
699
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>
697
705
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>
703
711
704
- <?php echo $view['form']->rest($form) ?>
712
+ <input type="submit" />
713
+
714
+ <?php echo $view['form']->end($form) ?>
705
715
706
716
If the auto-generated label for a field isn't quite right, you can explicitly
707
717
specify it:
@@ -777,8 +787,8 @@ that can be used with each.
777
787
Changing the Action and Method of a Form
778
788
----------------------------------------
779
789
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.
782
792
Sometimes you want to change these parameters. You can do so in a few different
783
793
ways. If you build your form in the controller, you can use ``setAction() `` and
784
794
``setMethod() ``::
@@ -795,16 +805,16 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
795
805
This example assumes that you've created a route called ``target_route ``
796
806
that points to the controller that processes the form.
797
807
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::
801
811
802
812
$form = $this->createForm(new TaskType(), $task, array(
803
813
'action' => $this->generateUrl('target_route'),
804
814
'method' => 'GET',
805
815
));
806
816
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
808
818
to the ``form() `` or the ``form_start() `` helper:
809
819
810
820
.. configuration-block ::
@@ -1100,7 +1110,6 @@ as the original ``Task`` fields:
1100
1110
{{ form_row(form.category.name) }}
1101
1111
</div>
1102
1112
1103
- {{ form_rest(form) }}
1104
1113
{# ... #}
1105
1114
1106
1115
.. code-block :: html+php
@@ -1112,7 +1121,6 @@ as the original ``Task`` fields:
1112
1121
<?php echo $view['form']->row($form['category']['name']) ?>
1113
1122
</div>
1114
1123
1115
- <?php echo $view['form']->rest($form) ?>
1116
1124
<!-- ... -->
1117
1125
1118
1126
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.
1480
1488
Symfony automatically validates the presence and accuracy of this token.
1481
1489
1482
1490
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
1484
1492
that all un-rendered fields are output.
1485
1493
1486
1494
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::
1543
1551
1544
1552
$form->handleRequest($request);
1545
1553
1546
- if ($form->isBound ()) {
1554
+ if ($form->isValid ()) {
1547
1555
// data is an array with "name", "email", and "message" keys
1548
1556
$data = $form->getData();
1549
1557
}
0 commit comments