Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a9d0e3a

Browse filesBrowse files
committed
Merge pull request symfony#2667 from gregquat/patch-15
Book/Form : Adding a new section about defining forms as service
2 parents a3b4695 + e134c1e commit a9d0e3a
Copy full SHA for a9d0e3a

File tree

Expand file treeCollapse file tree

1 file changed

+72
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+72
-0
lines changed

‎book/forms.rst

Copy file name to clipboardExpand all lines: book/forms.rst
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,78 @@ form "type"). It can be used to quickly build a form object in the controller::
833833
// ...
834834
}
835835

836+
.. tip::
837+
838+
Defining your form type as a service is a good practice and makes it easily usable in
839+
your application:
840+
841+
.. configuration-block::
842+
843+
.. code-block:: yaml
844+
845+
# src/Acme/TaskBundle/Resources/config/services.yml
846+
services:
847+
acme_demo.form.type.task:
848+
class: Acme\TaskBundle\Form\Type\TaskType
849+
tags:
850+
- { name: form.type, alias: task }
851+
852+
.. code-block:: xml
853+
854+
<!-- src/Acme/TaskBundle/Resources/config/services.xml -->
855+
<service id="acme_demo.form.type.task" class="Acme\TaskBundle\Form\Type\TaskType">
856+
<tag name="form.type" alias="task" />
857+
</service>
858+
859+
.. code-block:: php
860+
861+
// src/Acme/TaskBundle/Resources/config/services.php
862+
use Symfony\Component\DependencyInjection\Definition;
863+
864+
$container
865+
->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
866+
->addTag('form.type', array(
867+
'alias' => 'task',
868+
))
869+
;
870+
871+
That's it! Now you can use your form type directly in a controller::
872+
873+
// src/Acme/TaskBundle/Controller/DefaultController.php
874+
875+
// ...
876+
public function newAction()
877+
{
878+
$task = ...;
879+
$form = $this->createForm('task', $task);
880+
881+
// ...
882+
}
883+
884+
or even use it as a normal type in another form::
885+
886+
// src/Acme/TaskBundle/Form/Type/ListType.php
887+
namespace Acme\TaskBundle\Form\Type;
888+
889+
use Symfony\Component\Form\AbstractType;
890+
use Symfony\Component\Form\FormBuilderInterface;
891+
892+
class ListType extends AbstractType
893+
{
894+
public function buildForm(FormBuilderInterface $builder, array $options)
895+
{
896+
// ...
897+
898+
$builder->add('task', 'task');
899+
// Note that the property ``task`` (first argument)
900+
// is defined as a ``task`` form type (second).
901+
}
902+
903+
// ...
904+
}
905+
906+
Read :ref:`form-cookbook-form-field-service` for more information.
907+
836908
Placing the form logic into its own class means that the form can be easily
837909
reused elsewhere in your project. This is the best way to create forms, but
838910
the choice is ultimately up to you.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.