Skip to content

Commit 9852094

Browse files
committed
Merge branch 'xabbuh-issue-2969' into 2.3
2 parents b694313 + 1beb526 commit 9852094

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

components/form/introduction.rst

+43-49
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,23 @@ 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
76-
:class:`Symfony\\Component\\Form\\Extension\\HttpFoundation\\HttpFoundationExtension`
77-
to your form factory::
72+
To process form data, you'll need to call the :method:`Symfony\\Component\\Form\\Form::handleRequest`
73+
method::
74+
75+
$form->handleRequest();
76+
77+
Behind the scenes, this uses a :class:`Symfony\\Component\\Form\\NativeRequestHandler`
78+
object to read data off of the correct PHP superglobals (i.e. ``$_POST`` or
79+
``$_GET``) based on the HTTP method configured on the form (POST is default).
80+
81+
.. sidebar:: Integration with the HttpFoundation Component
82+
83+
If you use the HttpFoundation component, then you should add the
84+
:class:`Symfony\\Component\\Form\\Extension\\HttpFoundation\\HttpFoundationExtension`
85+
to your form factory::
7886

7987
use Symfony\Component\Form\Forms;
8088
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
@@ -83,14 +91,15 @@ to your form factory::
8391
->addExtension(new HttpFoundationExtension())
8492
->getFormFactory();
8593

86-
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.
94+
Now, when you process a form, you can pass the :class:`Symfony\\Component\\HttpFoundation\\Request`
95+
object to :method:`Symfony\\Component\\Form\\Form::handleRequest`::
8996

90-
.. note::
97+
$form->handleRequest($request);
9198

92-
For more information about the HttpFoundation component or how to
93-
install it, see :doc:`/components/http_foundation/introduction`.
99+
.. note::
100+
101+
For more information about the HttpFoundation component or how to
102+
install it, see :doc:`/components/http_foundation/introduction`.
94103

95104
CSRF Protection
96105
~~~~~~~~~~~~~~~
@@ -480,7 +489,7 @@ to do that in the ":ref:`form-rendering-template`" section.
480489
Handling Form Submissions
481490
~~~~~~~~~~~~~~~~~~~~~~~~~
482491

483-
To handle form submissions, use the :method:`Symfony\\Component\\Form\\Form::bind`
492+
To handle form submissions, use the :method:`Symfony\\Component\\Form\\Form::handleRequest`
484493
method:
485494

486495
.. configuration-block::
@@ -497,19 +506,17 @@ method:
497506
498507
$request = Request::createFromGlobals();
499508
500-
if ($request->isMethod('POST')) {
501-
$form->bind($request);
509+
$form->handleRequest($request);
502510
503-
if ($form->isValid()) {
504-
$data = $form->getData();
511+
if ($form->isValid()) {
512+
$data = $form->getData();
505513
506-
// ... perform some action, such as saving the data to the database
514+
// ... perform some action, such as saving the data to the database
507515
508-
$response = new RedirectResponse('/task/success');
509-
$response->prepare($request);
516+
$response = new RedirectResponse('/task/success');
517+
$response->prepare($request);
510518
511-
return $response->send();
512-
}
519+
return $response->send();
513520
}
514521
515522
// ...
@@ -525,17 +532,14 @@ method:
525532
->add('dueDate', 'date')
526533
->getForm();
527534
528-
// only process the form if the request is a POST request
529-
if ($request->isMethod('POST')) {
530-
$form->bind($request);
535+
$form->handleRequest($request);
531536
532-
if ($form->isValid()) {
533-
$data = $form->getData();
537+
if ($form->isValid()) {
538+
$data = $form->getData();
534539
535-
// ... perform some action, such as saving the data to the database
540+
// ... perform some action, such as saving the data to the database
536541
537-
return $this->redirect($this->generateUrl('task_success'));
538-
}
542+
return $this->redirect($this->generateUrl('task_success'));
539543
}
540544
541545
// ...
@@ -546,25 +550,15 @@ This defines a common form "workflow", which contains 3 different possibilities:
546550
1) On the initial GET request (i.e. when the user "surfs" to your page),
547551
build your form and render it;
548552

549-
If the request is a POST, process the submitted data (via ``bind``). Then:
550-
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()]);
553+
If the request is a POST, process the submitted data (via ``handleRequest()``).
554+
Then:
561555

562-
// ...
563-
}
556+
2) if the form is invalid, re-render the form (which will now contain errors);
557+
3) if the form is valid, perform some action and redirect.
564558

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``.
559+
Luckily, you don't need to decide whether or not a form has been submitted.
560+
Just pass the current request to the ``handleRequest()`` method. Then, the Form
561+
component will do all the necessary work for you.
568562

569563
.. _component-form-intro-validation:
570564

0 commit comments

Comments
 (0)