@@ -66,13 +66,13 @@ 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
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
76
76
:class: `Symfony\\ Component\\ Form\\ Extension\\ HttpFoundation\\ HttpFoundationExtension `
77
77
to your form factory::
78
78
@@ -84,8 +84,7 @@ to your form factory::
84
84
->getFormFactory();
85
85
86
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.
87
+ object to :method: `Symfony\\ Component\\ Form\\ Form::handleRequest `.
89
88
90
89
.. note ::
91
90
@@ -480,7 +479,7 @@ to do that in the ":ref:`form-rendering-template`" section.
480
479
Handling Form Submissions
481
480
~~~~~~~~~~~~~~~~~~~~~~~~~
482
481
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 `
484
483
method:
485
484
486
485
.. configuration-block ::
@@ -497,19 +496,17 @@ method:
497
496
498
497
$request = Request::createFromGlobals();
499
498
500
- if ($request->isMethod('POST')) {
501
- $form->bind($request);
499
+ $form->handleRequest($request);
502
500
503
- if ($form->isValid()) {
504
- $data = $form->getData();
501
+ if ($form->isValid()) {
502
+ $data = $form->getData();
505
503
506
- // ... perform some action, such as saving the data to the database
504
+ // ... perform some action, such as saving the data to the database
507
505
508
- $response = new RedirectResponse('/task/success');
509
- $response->prepare($request);
506
+ $response = new RedirectResponse('/task/success');
507
+ $response->prepare($request);
510
508
511
- return $response->send();
512
- }
509
+ return $response->send();
513
510
}
514
511
515
512
// ...
@@ -525,17 +522,14 @@ method:
525
522
->add('dueDate', 'date')
526
523
->getForm();
527
524
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);
531
526
532
- if ($form->isValid()) {
533
- $data = $form->getData();
527
+ if ($form->isValid()) {
528
+ $data = $form->getData();
534
529
535
- // ... perform some action, such as saving the data to the database
530
+ // ... perform some action, such as saving the data to the database
536
531
537
- return $this->redirect($this->generateUrl('task_success'));
538
- }
532
+ return $this->redirect($this->generateUrl('task_success'));
539
533
}
540
534
541
535
// ...
@@ -546,25 +540,15 @@ This defines a common form "workflow", which contains 3 different possibilities:
546
540
1) On the initial GET request (i.e. when the user "surfs" to your page),
547
541
build your form and render it;
548
542
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:
550
545
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.
564
548
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 .
568
552
569
553
.. _component-form-intro-validation :
570
554
0 commit comments