@@ -193,7 +193,7 @@ Handling Form Submissions
193
193
194
194
The second job of a form is to translate user-submitted data back to the
195
195
properties of an object. To make this happen, the submitted data from the
196
- user must be bound to the form. Add the following functionality to your
196
+ user must be written into the form. Add the following functionality to your
197
197
controller::
198
198
199
199
// ...
@@ -209,53 +209,54 @@ controller::
209
209
->add('dueDate', 'date')
210
210
->getForm();
211
211
212
- if ($request->isMethod('POST')) {
213
- $form->bind($request);
212
+ $form->handleRequest($request);
214
213
215
- if ($form->isValid()) {
216
- // perform some action, such as saving the task to the database
214
+ if ($form->isValid()) {
215
+ // perform some action, such as saving the task to the database
217
216
218
- return $this->redirect($this->generateUrl('task_success'));
219
- }
217
+ return $this->redirect($this->generateUrl('task_success'));
220
218
}
221
219
222
220
// ...
223
221
}
224
222
225
- .. versionadded :: 2.1
226
- The ``bind `` method was made more flexible in Symfony 2.1. It now accepts
227
- the raw client data (same as before) or a Symfony Request object. This
228
- is preferred over the deprecated ``bindRequest `` method.
229
-
230
- Now, when submitting the form, the controller binds the submitted data to the
231
- form, which translates that data back to the ``task `` and ``dueDate `` properties
232
- of the ``$task `` object. This all happens via the ``bind() `` method.
233
-
234
- .. note ::
235
-
236
- As soon as ``bind() `` is called, the submitted data is transferred
237
- to the underlying object immediately. This happens regardless of whether
238
- or not the underlying data is actually valid.
223
+ .. versionadded :: 2.3
224
+ 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.
239
227
240
228
This controller follows a common pattern for handling forms, and has three
241
229
possible paths:
242
230
243
- #. When initially loading the page in a browser, the request method is ``GET ``
244
- and the form is simply created and rendered;
231
+ #. When initially loading the page in a browser, the form is simply created and
232
+ rendered. :method: `Symfony\C omponent\F orm\F ormInterface::handleRequest `
233
+ recognizes that the form was not submitted and does nothing.
234
+ :method: `Symfony\C omponent\F orm\F ormInterface::isValid ` returns ``false ``
235
+ if the form was not submitted.
245
236
246
- #. When the user submits the form (i.e. the method is ``POST ``) with invalid
247
- data (validation is covered in the next section), the form is bound and
248
- then rendered, this time displaying all validation errors;
237
+ #. When the user submits the form, :method: `Symfony\C omponent\F orm\F ormInterface::handleRequest `
238
+ recognizes this and immediately writes the submitted data back into the
239
+ ``task `` and ``dueDate `` properties of the ``$task `` object. Then this object
240
+ is validated. If it is invalid (validation is covered in the next section),
241
+ :method: `Symfony\C omponent\F orm\F ormInterface::isValid ` returns ``false ``
242
+ again, so the form is rendered together with all validation errors;
249
243
250
- #. When the user submits the form with valid data, the form is bound and
251
- you have the opportunity to perform some actions using the ``$task ``
252
- object (e.g. persisting it to the database) before redirecting the user
253
- to some other page (e.g. a "thank you" or "success" page).
244
+ .. note ::
254
245
255
- .. note ::
246
+ You can use the method :method: `Symfony\C omponent\F orm\F ormInterface::isBound `
247
+ to check whether a form was submitted, regardless of whether or not the
248
+ submitted data is actually valid.
249
+
250
+ #. When the user submits the form with valid data, the submitted data is again
251
+ written into the form, but this time :method: `Symfony\C omponent\F orm\F ormInterface::isValid `
252
+ returns ``true ``. Now you have the opportunity to perform some actions using
253
+ the ``$task `` object (e.g. persisting it to the database) before redirecting
254
+ the user to some other page (e.g. a "thank you" or "success" page).
255
+
256
+ .. note ::
256
257
257
- Redirecting a user after a successful form submission prevents the user
258
- from being able to hit "refresh" and re-post the data.
258
+ Redirecting a user after a successful form submission prevents the user
259
+ from being able to hit "refresh" and re-post the data.
259
260
260
261
.. index ::
261
262
single: Forms; Validation
@@ -421,7 +422,7 @@ to an array callback, or a ``Closure``::
421
422
}
422
423
423
424
This will call the static method ``determineValidationGroups() `` on the
424
- ``Client `` class after the form is bound , but before validation is executed.
425
+ ``Client `` class after the form is submitted , but before validation is executed.
425
426
The Form object is passed as an argument to that method (see next example).
426
427
You can also define whole logic inline by using a Closure::
427
428
@@ -966,7 +967,7 @@ you can fetch it from the form::
966
967
967
968
For more information, see the :doc: `Doctrine ORM chapter</book/doctrine> `.
968
969
969
- The key thing to understand is that when the form is bound , the submitted
970
+ The key thing to understand is that when the form is submitted , the submitted
970
971
data is transferred to the underlying object immediately. If you want to
971
972
persist that data, you simply need to persist the object itself (which already
972
973
contains the submitted data).
@@ -1540,12 +1541,12 @@ an array of the submitted data. This is actually really easy::
1540
1541
->add('message', 'textarea')
1541
1542
->getForm();
1542
1543
1543
- if ($request->isMethod('POST')) {
1544
- $form->bind($request);
1544
+ $form->handleRequest($request);
1545
1545
1546
- // data is an array with "name", "email", and "message" keys
1547
- $data = $form->getData();
1548
- }
1546
+ if ($form->isBound()) {
1547
+ // data is an array with "name", "email", and "message" keys
1548
+ $data = $form->getData();
1549
+ }
1549
1550
1550
1551
// ... render the form
1551
1552
}
@@ -1580,15 +1581,15 @@ Adding Validation
1580
1581
1581
1582
The only missing piece is validation. Usually, when you call ``$form->isValid() ``,
1582
1583
the object is validated by reading the constraints that you applied to that
1583
- class. If your form is binding to an object (i.e. you're using the ``data_class ``
1584
+ class. If your form is mapped to an object (i.e. you're using the ``data_class ``
1584
1585
option or passing an object to your form), this is almost always the approach
1585
1586
you want to use. See :doc: `/book/validation ` for more details.
1586
1587
1587
1588
.. _form-option-constraints :
1588
1589
1589
- But if you're not binding to an object and are instead retrieving a simple
1590
- array of your submitted data, how can you add constraints to the data of your
1591
- form?
1590
+ But if the form is not mapped to an object and you instead want to retrieve a
1591
+ simple array of your submitted data, how can you add constraints to the data of
1592
+ your form?
1592
1593
1593
1594
The answer is to setup the constraints yourself, and attach them to the individual
1594
1595
fields. The overall approach is covered a bit more in the :ref: `validation chapter<book-validation-raw-values> `,
0 commit comments