@@ -66,15 +66,23 @@ factory.
66
66
Request Handling
67
67
~~~~~~~~~~~~~~~~
68
68
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.
74
71
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::
78
86
79
87
use Symfony\C omponent\F orm\F orms;
80
88
use Symfony\C omponent\F orm\E xtension\H ttpFoundation\H ttpFoundationExtension;
@@ -83,14 +91,15 @@ to your form factory::
83
91
->addExtension(new HttpFoundationExtension())
84
92
->getFormFactory();
85
93
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 `::
89
96
90
- .. note ::
97
+ $form->handleRequest($request);
91
98
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 `.
94
103
95
104
CSRF Protection
96
105
~~~~~~~~~~~~~~~
@@ -480,7 +489,7 @@ to do that in the ":ref:`form-rendering-template`" section.
480
489
Handling Form Submissions
481
490
~~~~~~~~~~~~~~~~~~~~~~~~~
482
491
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 `
484
493
method:
485
494
486
495
.. configuration-block ::
@@ -497,19 +506,17 @@ method:
497
506
498
507
$request = Request::createFromGlobals();
499
508
500
- if ($request->isMethod('POST')) {
501
- $form->bind($request);
509
+ $form->handleRequest($request);
502
510
503
- if ($form->isValid()) {
504
- $data = $form->getData();
511
+ if ($form->isValid()) {
512
+ $data = $form->getData();
505
513
506
- // ... perform some action, such as saving the data to the database
514
+ // ... perform some action, such as saving the data to the database
507
515
508
- $response = new RedirectResponse('/task/success');
509
- $response->prepare($request);
516
+ $response = new RedirectResponse('/task/success');
517
+ $response->prepare($request);
510
518
511
- return $response->send();
512
- }
519
+ return $response->send();
513
520
}
514
521
515
522
// ...
@@ -525,17 +532,14 @@ method:
525
532
->add('dueDate', 'date')
526
533
->getForm();
527
534
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);
531
536
532
- if ($form->isValid()) {
533
- $data = $form->getData();
537
+ if ($form->isValid()) {
538
+ $data = $form->getData();
534
539
535
- // ... perform some action, such as saving the data to the database
540
+ // ... perform some action, such as saving the data to the database
536
541
537
- return $this->redirect($this->generateUrl('task_success'));
538
- }
542
+ return $this->redirect($this->generateUrl('task_success'));
539
543
}
540
544
541
545
// ...
@@ -546,25 +550,15 @@ This defines a common form "workflow", which contains 3 different possibilities:
546
550
1) On the initial GET request (i.e. when the user "surfs" to your page),
547
551
build your form and render it;
548
552
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:
561
555
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.
564
558
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 .
568
562
569
563
.. _component-form-intro-validation :
570
564
0 commit comments