@@ -9,31 +9,108 @@ URL under which the form was rendered. Sometimes you want to change these
99parameters. You can do so in a few different ways.
1010
1111If 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() ``:
1313
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();
2165
2266 .. note ::
2367
2468 This example assumes that you've created a route called ``target_route ``
2569 that points to the controller that processes the form.
2670
2771When using a form type class, you can pass the action and method as form
28- options::
72+ options:
2973
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+ }
3298
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+ ));
37114
38115 Finally, you can override the action and method in the template by passing them
39116to the ``form() `` or the ``form_start() `` helper functions:
0 commit comments