@@ -9,31 +9,108 @@ URL under which the form was rendered. Sometimes you want to change these
9
9
parameters. You can do so in a few different ways.
10
10
11
11
If you use the :class: `Symfony\\ Component\\ Form\\ FormBuilder ` to build your
12
- form, you can use ``setAction() `` and ``setMethod() ``::
12
+ form, you can use ``setAction() `` and ``setMethod() ``:
13
13
14
- $form = $this->createFormBuilder($task)
15
- ->setAction($this->generateUrl('target_route'))
16
- ->setMethod('GET')
17
- ->add('task', TextType::class)
18
- ->add('dueDate', DateType::class)
19
- ->add('save', SubmitType::class)
20
- ->getForm();
14
+ .. configuration-block ::
15
+
16
+ .. code-block :: php-symfony
17
+
18
+ // AppBundle/Controller/DefaultController.php
19
+ namespace AppBundle\Controller;
20
+
21
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
22
+ use Symfony\Component\Form\Extension\Core\Type\DateType;
23
+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
24
+ use Symfony\Component\Form\Extension\Core\Type\TextType;
25
+
26
+ class DefaultController extends Controller
27
+ {
28
+ public function newAction()
29
+ {
30
+ $form = $this->createFormBuilder($task)
31
+ ->setAction($this->generateUrl('target_route'))
32
+ ->setMethod('GET')
33
+ ->add('task', TextType::class)
34
+ ->add('dueDate', DateType::class)
35
+ ->add('save', SubmitType::class)
36
+ ->getForm();
37
+
38
+ // ...
39
+ }
40
+ }
41
+
42
+ .. code-block :: php-standalone
43
+
44
+ use Symfony\Component\Form\Forms;
45
+ use Symfony\Component\Form\Extension\Core\Type\DateType;
46
+ use Symfony\Component\Form\Extension\Core\Type\FormType;
47
+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
48
+ use Symfony\Component\Form\Extension\Core\Type\TextType;
49
+
50
+ // ...
51
+
52
+ $formFactoryBuilder = Forms::createFormFactoryBuilder();
53
+
54
+ // Form factory builder configuration ...
55
+
56
+ $formFactory = $formFactoryBuilder->getFormFactory();
57
+
58
+ $form = $formFactory->createBuilder(FormType::class, $task)
59
+ ->setAction($this->generateUrl('target_route'))
60
+ ->setMethod('GET')
61
+ ->add('task', TextType::class)
62
+ ->add('dueDate', DateType::class)
63
+ ->add('save', SubmitType::class)
64
+ ->getForm();
21
65
22
66
.. note ::
23
67
24
68
This example assumes that you've created a route called ``target_route ``
25
69
that points to the controller that processes the form.
26
70
27
71
When using a form type class, you can pass the action and method as form
28
- options::
72
+ options:
29
73
30
- use AppBundle\Form\TaskType;
31
- // ...
74
+ .. configuration-block ::
75
+
76
+ .. code-block :: php-symfony
77
+
78
+ // AppBundle/Controller/DefaultController.php
79
+ namespace AppBundle\Controller;
80
+
81
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
82
+ use AppBundle\Form\TaskType;
83
+
84
+ class DefaultController extends Controller
85
+ {
86
+ public function newAction()
87
+ {
88
+ // ...
89
+
90
+ $form = $this->createForm(TaskType::class, $task, array(
91
+ 'action' => $this->generateUrl('target_route'),
92
+ 'method' => 'GET',
93
+ ));
94
+
95
+ // ...
96
+ }
97
+ }
32
98
33
- $form = $this->createForm(TaskType::class, $task, array(
34
- 'action' => $this->generateUrl('target_route'),
35
- 'method' => 'GET',
36
- ));
99
+ .. code-block :: php-standalone
100
+
101
+ use Symfony\Component\Form\Forms;
102
+ use AppBundle\Form\TaskType;
103
+
104
+ $formFactoryBuilder = Forms::createFormFactoryBuilder();
105
+
106
+ // Form factory builder configuration ...
107
+
108
+ $formFactory = $formFactoryBuilder->getFormFactory();
109
+
110
+ $form = $formFactory->create(TaskType::class, $task, array(
111
+ 'action' => $this->generateUrl('target_route'),
112
+ 'method' => 'GET',
113
+ ));
37
114
38
115
Finally, you can override the action and method in the template by passing them
39
116
to the ``form() `` or the ``form_start() `` helper functions:
0 commit comments