44How to Use the submit() Function to Handle Form Submissions
55===========================================================
66
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::
812
913 use Symfony\Component\HttpFoundation\Request;
1014 // ...
1115
1216 public function new(Request $request)
1317 {
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);
5419
5520 if ($request->isMethod('POST')) {
5621 $form->submit($request->request->get($form->getName()));
@@ -62,7 +27,7 @@ method, pass the submitted data directly to
6227 }
6328 }
6429
65- return $this->render('product /new.html.twig', [
30+ return $this->render('task /new.html.twig', [
6631 'form' => $form->createView(),
6732 ]);
6833 }
0 commit comments