4
4
How to Use the submit() Function to Handle Form Submissions
5
5
===========================================================
6
6
7
- Handle the form submission with the ``handleRequest() `` method::
7
+ The recommended way of :ref: `processing Symfony forms <processing-forms >` is to
8
+ use the :method: `Symfony\\ Component\\ Form\\ FormInterface::handleRequest ` method
9
+ to detect when the form has been submitted. However, you can also use the
10
+ :method: `Symfony\\ Component\\ Form\\ FormInterface::submit ` method to have better
11
+ control over when exactly your form is submitted and what data is passed to it::
8
12
9
13
use Symfony\Component\HttpFoundation\Request;
10
14
// ...
11
15
12
16
public function new(Request $request)
13
17
{
14
- $form = $this->createFormBuilder()
15
- // ...
16
- ->getForm();
17
-
18
- $form->handleRequest($request);
19
-
20
- if ($form->isSubmitted() && $form->isValid()) {
21
- // perform some action...
22
-
23
- return $this->redirectToRoute('task_success');
24
- }
25
-
26
- return $this->render('product/new.html.twig', [
27
- 'form' => $form->createView(),
28
- ]);
29
- }
30
-
31
- .. tip ::
32
-
33
- To see more about this method, read :ref: `form-handling-form-submissions `.
34
-
35
- .. _form-call-submit-directly :
36
-
37
- Calling Form::submit() manually
38
- -------------------------------
39
-
40
- In some cases, you want better control over when exactly your form is submitted
41
- and what data is passed to it. Instead of using the
42
- :method: `Symfony\\ Component\\ Form\\ FormInterface::handleRequest `
43
- method, pass the submitted data directly to
44
- :method: `Symfony\\ Component\\ Form\\ FormInterface::submit `::
45
-
46
- use Symfony\Component\HttpFoundation\Request;
47
- // ...
48
-
49
- public function new(Request $request)
50
- {
51
- $form = $this->createFormBuilder()
52
- // ...
53
- ->getForm();
18
+ $form = $this->createForm(TaskType::class, $task);
54
19
55
20
if ($request->isMethod('POST')) {
56
21
$form->submit($request->request->get($form->getName()));
@@ -62,7 +27,7 @@ method, pass the submitted data directly to
62
27
}
63
28
}
64
29
65
- return $this->render('product /new.html.twig', [
30
+ return $this->render('task /new.html.twig', [
66
31
'form' => $form->createView(),
67
32
]);
68
33
}
0 commit comments