Skip to content

Commit 0763900

Browse files
committed
Documented the usage of FormInterface::handleRequest
1 parent 318ba88 commit 0763900

File tree

6 files changed

+89
-95
lines changed

6 files changed

+89
-95
lines changed

book/controller.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ working with forms, for example::
325325
{
326326
$form = $this->createForm(...);
327327

328-
$form->bind($request);
328+
$form->handleRequest($request);
329329
// ...
330330
}
331331

@@ -663,7 +663,8 @@ For example, imagine you're processing a form submit::
663663
{
664664
$form = $this->createForm(...);
665665

666-
$form->bind($this->getRequest());
666+
$form->handleRequest($this->getRequest());
667+
667668
if ($form->isValid()) {
668669
// do some sort of processing
669670

book/forms.rst

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Handling Form Submissions
193193

194194
The second job of a form is to translate user-submitted data back to the
195195
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
197197
controller::
198198

199199
// ...
@@ -209,53 +209,54 @@ controller::
209209
->add('dueDate', 'date')
210210
->getForm();
211211

212-
if ($request->isMethod('POST')) {
213-
$form->bind($request);
212+
$form->handleRequest($request);
214213

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
217216

218-
return $this->redirect($this->generateUrl('task_success'));
219-
}
217+
return $this->redirect($this->generateUrl('task_success'));
220218
}
221219

222220
// ...
223221
}
224222

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\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.
239227

240228
This controller follows a common pattern for handling forms, and has three
241229
possible paths:
242230

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\Component\Form\FormInterface::handleRequest`
233+
recognizes that the form was not submitted and does nothing.
234+
:method:`Symfony\Component\Form\FormInterface::isValid` returns ``false``
235+
if the form was not submitted.
245236

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\Component\Form\FormInterface::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\Component\Form\FormInterface::isValid` returns ``false``
242+
again, so the form is rendered together with all validation errors;
249243

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::
254245

255-
.. note::
246+
You can use the method :method:`Symfony\Component\Form\FormInterface::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\Component\Form\FormInterface::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::
256257

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.
259260

260261
.. index::
261262
single: Forms; Validation
@@ -421,7 +422,7 @@ to an array callback, or a ``Closure``::
421422
}
422423

423424
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.
425426
The Form object is passed as an argument to that method (see next example).
426427
You can also define whole logic inline by using a Closure::
427428

@@ -966,7 +967,7 @@ you can fetch it from the form::
966967

967968
For more information, see the :doc:`Doctrine ORM chapter</book/doctrine>`.
968969

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
970971
data is transferred to the underlying object immediately. If you want to
971972
persist that data, you simply need to persist the object itself (which already
972973
contains the submitted data).
@@ -1540,12 +1541,12 @@ an array of the submitted data. This is actually really easy::
15401541
->add('message', 'textarea')
15411542
->getForm();
15421543

1543-
if ($request->isMethod('POST')) {
1544-
$form->bind($request);
1544+
$form->handleRequest($request);
15451545

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+
}
15491550

15501551
// ... render the form
15511552
}
@@ -1580,15 +1581,15 @@ Adding Validation
15801581

15811582
The only missing piece is validation. Usually, when you call ``$form->isValid()``,
15821583
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``
15841585
option or passing an object to your form), this is almost always the approach
15851586
you want to use. See :doc:`/book/validation` for more details.
15861587

15871588
.. _form-option-constraints:
15881589

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?
15921593

15931594
The answer is to setup the constraints yourself, and attach them to the individual
15941595
fields. The overall approach is covered a bit more in the :ref:`validation chapter<book-validation-raw-values>`,

book/validation.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,12 @@ workflow looks like the following from inside a controller::
220220
$author = new Author();
221221
$form = $this->createForm(new AuthorType(), $author);
222222

223-
if ($request->isMethod('POST')) {
224-
$form->bind($request);
223+
$form->handleRequest($request);
225224

226-
if ($form->isValid()) {
227-
// the validation passed, do something with the $author object
225+
if ($form->isValid()) {
226+
// the validation passed, do something with the $author object
228227

229-
return $this->redirect($this->generateUrl(...));
230-
}
228+
return $this->redirect($this->generateUrl(...));
231229
}
232230

233231
return $this->render('BlogBundle:Author:form.html.twig', array(

cookbook/doctrine/file_uploads.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,23 @@ The following controller shows you how to handle the entire process::
227227
/**
228228
* @Template()
229229
*/
230-
public function uploadAction()
230+
public function uploadAction(Request $request)
231231
{
232232
$document = new Document();
233233
$form = $this->createFormBuilder($document)
234234
->add('name')
235235
->add('file')
236-
->getForm()
237-
;
236+
->getForm();
238237

239-
if ($this->getRequest()->isMethod('POST')) {
240-
$form->bind($this->getRequest());
241-
if ($form->isValid()) {
242-
$em = $this->getDoctrine()->getManager();
238+
$form->handleRequest($request);
243239

244-
$em->persist($document);
245-
$em->flush();
240+
if ($form->isValid()) {
241+
$em = $this->getDoctrine()->getManager();
246242

247-
return $this->redirect($this->generateUrl(...));
248-
}
243+
$em->persist($document);
244+
$em->flush();
245+
246+
return $this->redirect($this->generateUrl(...));
249247
}
250248

251249
return array('form' => $form->createView());

cookbook/doctrine/registration_form.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@ and its template:
254254
Finally, create the controller which handles the form submission. This performs
255255
the validation and saves the data into the database::
256256

257-
public function createAction()
257+
public function createAction(Request $request)
258258
{
259259
$em = $this->getDoctrine()->getEntityManager();
260260

261261
$form = $this->createForm(new RegistrationType(), new Registration());
262262

263-
$form->bind($this->getRequest());
263+
$form->handleRequest($request);
264264

265265
if ($form->isValid()) {
266266
$registration = $form->getData();

cookbook/form/form_collections.rst

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,10 @@ In your controller, you'll now initialize a new instance of ``TaskType``::
177177

178178
$form = $this->createForm(new TaskType(), $task);
179179

180-
// process the form on POST
181-
if ($request->isMethod('POST')) {
182-
$form->bind($request);
183-
if ($form->isValid()) {
184-
// ... maybe do some form processing, like saving the Task and Tag objects
185-
}
180+
$form->handleRequest($request);
181+
182+
if ($form->isValid()) {
183+
// ... maybe do some form processing, like saving the Task and Tag objects
186184
}
187185

188186
return $this->render('AcmeTaskBundle:Task:new.html.twig', array(
@@ -642,40 +640,38 @@ the relationship between the removed ``Tag`` and ``Task`` object.
642640

643641
$editForm = $this->createForm(new TaskType(), $task);
644642

645-
if ($request->isMethod('POST')) {
646-
$editForm->bind($this->getRequest());
643+
$editForm->handleRequest($request);
647644

648-
if ($editForm->isValid()) {
645+
if ($editForm->isValid()) {
649646

650-
// filter $originalTags to contain tags no longer present
651-
foreach ($task->getTags() as $tag) {
652-
foreach ($originalTags as $key => $toDel) {
653-
if ($toDel->getId() === $tag->getId()) {
654-
unset($originalTags[$key]);
655-
}
647+
// filter $originalTags to contain tags no longer present
648+
foreach ($task->getTags() as $tag) {
649+
foreach ($originalTags as $key => $toDel) {
650+
if ($toDel->getId() === $tag->getId()) {
651+
unset($originalTags[$key]);
656652
}
657653
}
654+
}
658655

659-
// remove the relationship between the tag and the Task
660-
foreach ($originalTags as $tag) {
661-
// remove the Task from the Tag
662-
$tag->getTasks()->removeElement($task);
656+
// remove the relationship between the tag and the Task
657+
foreach ($originalTags as $tag) {
658+
// remove the Task from the Tag
659+
$tag->getTasks()->removeElement($task);
663660

664-
// if it were a ManyToOne relationship, remove the relationship like this
665-
// $tag->setTask(null);
661+
// if it were a ManyToOne relationship, remove the relationship like this
662+
// $tag->setTask(null);
666663

667-
$em->persist($tag);
664+
$em->persist($tag);
668665

669-
// if you wanted to delete the Tag entirely, you can also do that
670-
// $em->remove($tag);
671-
}
666+
// if you wanted to delete the Tag entirely, you can also do that
667+
// $em->remove($tag);
668+
}
672669

673-
$em->persist($task);
674-
$em->flush();
670+
$em->persist($task);
671+
$em->flush();
675672

676-
// redirect back to some edit page
677-
return $this->redirect($this->generateUrl('task_edit', array('id' => $id)));
678-
}
673+
// redirect back to some edit page
674+
return $this->redirect($this->generateUrl('task_edit', array('id' => $id)));
679675
}
680676

681677
// render some form template

0 commit comments

Comments
 (0)