1
1
.. index ::
2
- single: Form; Form::bind
2
+ single: Form; Form::submit()
3
3
4
- How to use the bind Function to handle Form Submissions
5
- =======================================================
4
+ How to use the submit() Function to handle Form Submissions
5
+ ===========================================================
6
6
7
7
In Symfony 2.3, a new :method: `Symfony\C omponent\F orm\F ormInterface::handleRequest `
8
8
method was added, which makes handling form submissions easier than ever::
@@ -33,11 +33,14 @@ method was added, which makes handling form submissions easier than ever::
33
33
34
34
To see more about this method, read :ref: `book-form-handling-form-submissions `.
35
35
36
- Using Form::bind () to handle a request (deprecated)
37
- ---------------------------------------------------
36
+ Calling Form::submit () manually
37
+ -------------------------------
38
38
39
- Prior to this, the :method: `Symfony\C omponent\F orm\F ormInterface::bind ` method
40
- was used instead::
39
+ In some cases, you want better control over when exactly your form is submitted
40
+ and what data is passed to it. Instead of using the
41
+ :method: `Symfony\C omponent\F orm\F ormInterface::handleRequest `
42
+ method, pass the submitted data directly to
43
+ :method: `Symfony\C omponent\F orm\F ormInterface::submit `::
41
44
42
45
use Symfony\Component\HttpFoundation\Request;
43
46
// ...
@@ -49,7 +52,7 @@ was used instead::
49
52
->getForm();
50
53
51
54
if ($request->isMethod('POST')) {
52
- $form->bind ($request);
55
+ $form->submit ($request->request->get($form->getName()) );
53
56
54
57
if ($form->isValid()) {
55
58
// perform some action...
@@ -63,17 +66,21 @@ was used instead::
63
66
));
64
67
}
65
68
66
- Passing the :class: `Symfony\\ Component\H ttpFoundation\\ Request ` directly to
67
- ``bind `` still works, but is deprecated and will be removed in Symfony 3.0.
68
- However, you *can * safely pass array values directly to bind.
69
+ .. tip ::
70
+
71
+ Forms consisting of nested fields expect an array in
72
+ :method: `Symfony\C omponent\F orm\F ormInterface::submit `. You can also submit
73
+ individual fields by calling :method: `Symfony\C omponent\F orm\F ormInterface::submit `
74
+ directly on the field::
69
75
70
- Passing an Array directly to Form::bind
71
- ---------------------------------------
76
+ $form->get('firstName')->submit('Fabien');
72
77
73
- In some cases, you may want to collect and pass an array of values directly
74
- to a Form, instead of using the ``handleRequest `` method. This is absolutely
75
- valid and not deprecated (passing a :class: `Symfony\\ Component\H ttpFoundation\\ Request `
76
- object to ``Form::bind `` is deprecated, but passing an array of ok)::
78
+ Passing a Request to Form::submit() (deprecated)
79
+ ------------------------------------------------
80
+
81
+ Before Symfony 2.3, the :method: `Symfony\C omponent\F orm\F ormInterface::submit `
82
+ method accepted a :class: `Symfony\\ Component\\ HttpFoundation\\ Reuqest ` object as
83
+ a convenient shortcut to the previous example::
77
84
78
85
use Symfony\Component\HttpFoundation\Request;
79
86
// ...
@@ -85,7 +92,7 @@ object to ``Form::bind`` is deprecated, but passing an array of ok)::
85
92
->getForm();
86
93
87
94
if ($request->isMethod('POST')) {
88
- $form->bind ($request->request->get($form->getName()) );
95
+ $form->submit ($request);
89
96
90
97
if ($form->isValid()) {
91
98
// perform some action...
@@ -98,3 +105,8 @@ object to ``Form::bind`` is deprecated, but passing an array of ok)::
98
105
'form' => $form->createView(),
99
106
));
100
107
}
108
+
109
+ Passing the :class: `Symfony\\ Component\H ttpFoundation\\ Request ` directly to
110
+ :method: `Symfony\\ Component\\ Form\\ FormInterface::submit` ` still works, but is
111
+ deprecated and will be removed in Symfony 3.0. You should use the method
112
+ :method: `Symfony\C omponent\F orm\F ormInterface::handleRequest ` instead.
0 commit comments