Skip to content

Commit 7336433

Browse files
weaverryanxabbuh
authored andcommitted
Moving out forms as services out of book
1 parent 5b8073e commit 7336433

File tree

2 files changed

+164
-83
lines changed

2 files changed

+164
-83
lines changed

form/form_dependencies.rst

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
.. _form-as-services:
2+
3+
How to Access Services or Config from Inside a Form
4+
===================================================
5+
6+
Sometimes, you may need to access a :doc:`services </service_container>` or other
7+
configuration from inside of your form class. To do this, you have 2 options:
8+
9+
1) Pass Options to your Form
10+
----------------------------
11+
12+
The simplest way to pass services or configuration to your form is via form *options*.
13+
Suppose you need to access the ``doctrine.orm.entity_manager`` service so that you
14+
can make a query. First, allow (in fact, require) a new ``entity_manager`` option
15+
to be passed to your form::
16+
17+
// src/AppBundle/Form/TaskType.php
18+
// ...
19+
20+
class TaskType extends AbstractType
21+
{
22+
// ...
23+
24+
public function configureOptions(OptionsResolver $resolver)
25+
{
26+
// ...
27+
28+
$resolver->setRequired('entity_manager');
29+
}
30+
}
31+
32+
Now that you've done this, you *must* pass an ``entity_manager`` option when you
33+
create your form::
34+
35+
// src/AppBundle/Controller/DefaultController.php
36+
// ...
37+
38+
public function newAction()
39+
{
40+
$task = ...;
41+
$form = $this->createForm(new TaskType(), $task, array(
42+
'entity_manager' => $this->get('doctrine.orm.entity_manager')
43+
));
44+
45+
// ...
46+
}
47+
48+
Finally, the ``entity_manager`` option is accessible in the ``$options`` argument
49+
if your ``buildForm`` method::
50+
51+
// src/AppBundle/Form/TaskType.php
52+
// ...
53+
54+
class TaskType extends AbstractType
55+
{
56+
public function buildForm(FormBuilderInterface $builder, array $options)
57+
{
58+
/** @var \Doctrine\ORM\EntityManager $em */
59+
$em = $options['entity_manager'];
60+
// ...
61+
}
62+
63+
// ...
64+
}
65+
66+
Use this method to pass *anything* to your form.
67+
68+
2) Define your Form as a Service
69+
--------------------------------
70+
71+
Alternatively, you can define your form class as a service. This is a good idea if
72+
you want to re-use the form in several places - registering it as a service makes
73+
this easier.
74+
75+
Suppose you need to access the ``doctrine.orm.entity_manager`` service so that you
76+
can make a query. First, add this as an argument to your form class::
77+
78+
// src/AppBundle/Form/TaskType.php
79+
80+
use Doctrine\ORM\EntityManager;
81+
// ...
82+
83+
class TaskType extends AbstractType
84+
{
85+
private $em;
86+
87+
public function __construct(EntityManager $em)
88+
{
89+
$this->em = $em;
90+
}
91+
92+
// ...
93+
}
94+
95+
Next, register this as a service and tag it with ``form.type``:
96+
97+
.. configuration-block::
98+
99+
.. code-block:: yaml
100+
101+
# src/AppBundle/Resources/config/services.yml
102+
services:
103+
app.form.type.task:
104+
class: AppBundle\Form\TaskType
105+
tags:
106+
- { name: form.type, alias: app_task }
107+
108+
.. code-block:: xml
109+
110+
<!-- src/AppBundle/Resources/config/services.xml -->
111+
<?xml version="1.0" encoding="UTF-8" ?>
112+
<container xmlns="http://symfony.com/schema/dic/services"
113+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
114+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
115+
116+
<services>
117+
<service id="app.form.type.task" class="AppBundle\Form\TaskType">
118+
<tag name="form.type" alias="app_task" />
119+
</service>
120+
</services>
121+
</container>
122+
123+
.. code-block:: php
124+
125+
// src/AppBundle/Resources/config/services.php
126+
$container
127+
->register(
128+
'app.form.type.task',
129+
'AppBundle\Form\TaskType'
130+
)
131+
->addTag('form.type', array(
132+
'alias' => 'app_task',
133+
))
134+
;
135+
136+
That's it! Use the ``alias`` key from the tag to reference your form::
137+
138+
// src/AppBundle/Controller/DefaultController.php
139+
// ...
140+
141+
public function newAction()
142+
{
143+
$task = ...;
144+
$form = $this->createForm('app_task', $task);
145+
146+
// ...
147+
}
148+
149+
Or, use the from within another form::
150+
151+
// src/AppBundle/Form/Type/ListType.php
152+
// ...
153+
154+
class ListType extends AbstractType
155+
{
156+
public function buildForm(FormBuilderInterface $builder, array $options)
157+
{
158+
// ...
159+
160+
$builder->add('someTask', 'app_task');
161+
}
162+
}
163+
164+
Read :ref:`form-cookbook-form-field-service` for more information.

forms.rst

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -690,89 +690,6 @@ the choice is ultimately up to you.
690690

691691
$form->get('dueDate')->setData(new \DateTime());
692692

693-
.. _form-as-services:
694-
695-
Defining your Forms as Services
696-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
697-
698-
Defining your form type as a service is a good practice and makes it really
699-
easy to use in your application.
700-
701-
.. note::
702-
703-
Services and the service container will be handled
704-
:doc:`later on in this book </service_container>`. Things will be
705-
more clear after reading that chapter.
706-
707-
.. configuration-block::
708-
709-
.. code-block:: yaml
710-
711-
# src/AppBundle/Resources/config/services.yml
712-
services:
713-
app.form.type.task:
714-
class: AppBundle\Form\TaskType
715-
tags:
716-
- { name: form.type, alias: app_task }
717-
718-
.. code-block:: xml
719-
720-
<!-- src/AppBundle/Resources/config/services.xml -->
721-
<?xml version="1.0" encoding="UTF-8" ?>
722-
<container xmlns="http://symfony.com/schema/dic/services"
723-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
724-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
725-
726-
<services>
727-
<service id="app.form.type.task" class="AppBundle\Form\TaskType">
728-
<tag name="form.type" alias="app_task" />
729-
</service>
730-
</services>
731-
</container>
732-
733-
.. code-block:: php
734-
735-
// src/AppBundle/Resources/config/services.php
736-
$container
737-
->register(
738-
'app.form.type.task',
739-
'AppBundle\Form\TaskType'
740-
)
741-
->addTag('form.type', array(
742-
'alias' => 'app_task',
743-
))
744-
;
745-
746-
That's it! Now you can use your form type directly in a controller::
747-
748-
// src/AppBundle/Controller/DefaultController.php
749-
// ...
750-
751-
public function newAction()
752-
{
753-
$task = ...;
754-
$form = $this->createForm('task', $task);
755-
756-
// ...
757-
}
758-
759-
or even use from within the form type of another form::
760-
761-
// src/AppBundle/Form/Type/ListType.php
762-
// ...
763-
764-
class ListType extends AbstractType
765-
{
766-
public function buildForm(FormBuilderInterface $builder, array $options)
767-
{
768-
// ...
769-
770-
$builder->add('someTask', 'task');
771-
}
772-
}
773-
774-
Read :ref:`form-cookbook-form-field-service` for more information.
775-
776693
.. index::
777694
pair: Forms; Doctrine
778695

0 commit comments

Comments
 (0)