Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce15629

Browse files
Merge pull request #1 from matthieu88160/matthieu88160-patch-3
Improve example context
2 parents 89cba1e + d1afeea commit ce15629

File tree

1 file changed

+98
-13
lines changed

1 file changed

+98
-13
lines changed

form/action_method.rst

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,61 @@ parameters. You can do so in a few different ways.
1111
If you use the :class:`Symfony\\Component\\Form\\FormBuilder` to build your
1212
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\TextType;
23+
use Symfony\Component\Form\Extension\Core\Type\DateType;
24+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
25+
26+
class DefaultController extends Controller
27+
{
28+
public function newAction()
29+
{
30+
31+
// ...
32+
33+
$form = $this->createFormBuilder($task)
34+
->setAction($this->generateUrl('target_route'))
35+
->setMethod('GET')
36+
->add('task', TextType::class)
37+
->add('dueDate', DateType::class)
38+
->add('save', SubmitType::class)
39+
->getForm();
40+
41+
// ...
42+
}
43+
}
44+
45+
46+
.. code-block:: php-standalone
47+
48+
use Symfony\Component\Form\Forms;
49+
use Symfony\Component\Form\Extension\Core\Type\FormType;
50+
use Symfony\Component\Form\Extension\Core\Type\TextType;
51+
use Symfony\Component\Form\Extension\Core\Type\DateType;
52+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
53+
54+
// ...
55+
56+
$formFactoryBuilder = Forms::createFormFactoryBuilder();
57+
58+
// Form factory builder configuration ...
59+
60+
$formFactory = $formFactoryBuilder->getFormFactory();
61+
62+
$form = $formFactory->createBuilder(FormType::class, $task)
63+
->setAction($this->generateUrl('target_route'))
64+
->setMethod('GET')
65+
->add('task', TextType::class)
66+
->add('dueDate', DateType::class)
67+
->add('save', SubmitType::class)
68+
->getForm();
2169
2270
.. note::
2371

@@ -27,13 +75,50 @@ form, you can use ``setAction()`` and ``setMethod()``::
2775
When using a form type class, you can pass the action and method as form
2876
options::
2977

30-
use AppBundle\Form\TaskType;
31-
// ...
78+
.. configuration-block::
79+
80+
.. code-block:: php-symfony
81+
82+
// AppBundle/Controller/DefaultController.php
83+
namespace AppBundle\Controller;
84+
85+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
86+
use AppBundle\Form\TaskType;
87+
88+
class DefaultController extends Controller
89+
{
90+
public function newAction()
91+
{
92+
// ...
93+
94+
$form = $this->createForm(TaskType::class, $task, array(
95+
'action' => $this->generateUrl('target_route'),
96+
'method' => 'GET',
97+
));
3298
33-
$form = $this->createForm(TaskType::class, $task, array(
34-
'action' => $this->generateUrl('target_route'),
35-
'method' => 'GET',
36-
));
99+
// ...
100+
}
101+
}
102+
103+
104+
.. code-block:: php-standalone
105+
106+
use Symfony\Component\Form\Forms;
107+
use Symfony\Component\Form\Extension\Core\Type\TextType;
108+
use Symfony\Component\Form\Extension\Core\Type\DateType;
109+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
110+
use AppBundle\Form\TaskType;
111+
112+
$formFactoryBuilder = Forms::createFormFactoryBuilder();
113+
114+
// Form factory builder configuration ...
115+
116+
$formFactory = $formFactoryBuilder->getFormFactory();
117+
118+
$form = $formFactory->create(TaskType::class, $task, array(
119+
'action' => $this->generateUrl('target_route'),
120+
'method' => 'GET',
121+
));
37122
38123
Finally, you can override the action and method in the template by passing them
39124
to the ``form()`` or the ``form_start()`` helper functions:

0 commit comments

Comments
 (0)