Skip to content

Commit a5c4a4f

Browse files
committed
use handleRequest() instead of bind()
1 parent 3368509 commit a5c4a4f

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

components/form/introduction.rst

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ factory.
6666
Request Handling
6767
~~~~~~~~~~~~~~~~
6868

69-
To process form data, you'll need to grab information off of the request (typically
70-
``$_POST`` data) and pass the array of submitted data to
71-
:method:`Symfony\\Component\\Form\\Form::bind`. The Form component optionally
72-
integrates with Symfony's :doc:`HttpFoundation </components/http_foundation/introduction>`
73-
component to make this even easier.
69+
.. versionadded:: 2.3
70+
The ``handleRequest()`` method was added in Symfony 2.3.
7471

75-
To integrate the HttpFoundation component, add the
72+
To process form data, you'll need to call the :method:`Symfony\\Component\\Form\\Form::handleRequest`
73+
method.
74+
75+
To optionally integrate the HttpFoundation component, add the
7676
:class:`Symfony\\Component\\Form\\Extension\\HttpFoundation\\HttpFoundationExtension`
7777
to your form factory::
7878

@@ -84,8 +84,7 @@ to your form factory::
8484
->getFormFactory();
8585

8686
Now, when you process a form, you can pass the :class:`Symfony\\Component\\HttpFoundation\\Request`
87-
object to :method:`Symfony\\Component\\Form\\Form::bind` instead of the raw
88-
array of submitted values.
87+
object to :method:`Symfony\\Component\\Form\\Form::handleRequest`.
8988

9089
.. note::
9190

@@ -480,7 +479,7 @@ to do that in the ":ref:`form-rendering-template`" section.
480479
Handling Form Submissions
481480
~~~~~~~~~~~~~~~~~~~~~~~~~
482481

483-
To handle form submissions, use the :method:`Symfony\\Component\\Form\\Form::bind`
482+
To handle form submissions, use the :method:`Symfony\\Component\\Form\\Form::handleRequest`
484483
method:
485484

486485
.. configuration-block::
@@ -497,19 +496,17 @@ method:
497496
498497
$request = Request::createFromGlobals();
499498
500-
if ($request->isMethod('POST')) {
501-
$form->bind($request);
499+
$form->handleRequest($request);
502500
503-
if ($form->isValid()) {
504-
$data = $form->getData();
501+
if ($form->isValid()) {
502+
$data = $form->getData();
505503
506-
// ... perform some action, such as saving the data to the database
504+
// ... perform some action, such as saving the data to the database
507505
508-
$response = new RedirectResponse('/task/success');
509-
$response->prepare($request);
506+
$response = new RedirectResponse('/task/success');
507+
$response->prepare($request);
510508
511-
return $response->send();
512-
}
509+
return $response->send();
513510
}
514511
515512
// ...
@@ -525,17 +522,14 @@ method:
525522
->add('dueDate', 'date')
526523
->getForm();
527524
528-
// only process the form if the request is a POST request
529-
if ($request->isMethod('POST')) {
530-
$form->bind($request);
525+
$form->handleRequest($request);
531526
532-
if ($form->isValid()) {
533-
$data = $form->getData();
527+
if ($form->isValid()) {
528+
$data = $form->getData();
534529
535-
// ... perform some action, such as saving the data to the database
530+
// ... perform some action, such as saving the data to the database
536531
537-
return $this->redirect($this->generateUrl('task_success'));
538-
}
532+
return $this->redirect($this->generateUrl('task_success'));
539533
}
540534
541535
// ...
@@ -546,25 +540,15 @@ This defines a common form "workflow", which contains 3 different possibilities:
546540
1) On the initial GET request (i.e. when the user "surfs" to your page),
547541
build your form and render it;
548542

549-
If the request is a POST, process the submitted data (via ``bind``). Then:
543+
If the request is a POST, process the submitted data (via ``handleRequest()``).
544+
Then:
550545

551-
2) if the form is invalid, re-render the form (which will now contain errors)
552-
3) if the form is valid, perform some action and redirect;
553-
554-
.. note::
555-
556-
If you're not using HttpFoundation, just pass the POST'ed data directly
557-
to ``bind``::
558-
559-
if (isset($_POST[$form->getName()])) {
560-
$form->bind($_POST[$form->getName()]);
561-
562-
// ...
563-
}
546+
2) if the form is invalid, re-render the form (which will now contain errors);
547+
3) if the form is valid, perform some action and redirect.
564548

565-
If you're uploading files, you'll need to do a little bit more work by
566-
merging the ``$_POST`` array with the ``$_FILES`` array before passing
567-
it into ``bind``.
549+
Luckily, you don't need to decide whether or not a form has been submitted.
550+
Just pass the current request to the ``handleRequest()`` method. Then, the Form
551+
component will do all the necessary work for you.
568552

569553
.. _component-form-intro-validation:
570554

0 commit comments

Comments
 (0)