From 9f0c2f14bc5afc547ddd45641bfb872660d31950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Quatannens?= Date: Mon, 27 May 2013 14:03:57 +0200 Subject: [PATCH 1/4] Book/Form : Adding a new section about defining forms as service | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed ticket | - --- book/forms.rst | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/book/forms.rst b/book/forms.rst index 9f8f8a1b7ac..bbb25b090bc 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -833,6 +833,78 @@ form "type"). It can be used to quickly build a form object in the controller:: // ... } +.. tip:: + + Defining your form as a service is a good practice and make it easily usable in + your application. First, you must tag your form as a form type in the service container: + + .. configuration-block:: + + .. code-block:: yaml + + # src/Acme/TaskBundle/Resources/config/services.yml + services: + acme_demo.form.type.task: + class: Acme\TaskBundle\Form\Type\TaskType + tags: + - { name: form.type, alias: task } + + .. code-block:: xml + + + + + + + .. code-block:: php + + // src/Acme/TaskBundle/Resources/config/services.php + use Symfony\Component\DependencyInjection\Definition; + + $container + ->setDefinition('acme_demo.form.type.task', new Definition( + 'Acme\TaskBundle\Form\Type\TaskType' + ) + ->addTag('form.type', array( + 'alias' => 'task', + )) + ; + + That's it! Now you can use your form directly in a controller:: + + // src/Acme/TaskBundle/Controller/DefaultController.php + + public function newAction() + { + $task = ...; + $form = $this->createForm('task', $task); + + // ... + } + + or even use it as a normal type in another form:: + + // src/Acme/TaskBundle/Form/Type/ListType.php + namespace Acme\TaskBundle\Form\Type; + + use Symfony\Component\Form\AbstractType; + use Symfony\Component\Form\FormBuilderInterface; + + class ListType extends AbstractType + { + public function buildForm(FormBuilderInterface $builder, array $options) + { + // ... + + $builder->add('task', 'task'); + // Note that the property ``task`` (first argument) is defined as a ``task`` form type (second). + } + + // ... + } + + Read :ref:`form-cookbook-form-field-service` for more information. + Placing the form logic into its own class means that the form can be easily reused elsewhere in your project. This is the best way to create forms, but the choice is ultimately up to you. From 83b6c8f937dd3c9dcf5ee9fbdb473c09ca48bf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Quatannens?= Date: Mon, 27 May 2013 15:57:14 +0300 Subject: [PATCH 2/4] Clarifying form / form type --- book/forms.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index bbb25b090bc..4f72a86fe3c 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -835,8 +835,8 @@ form "type"). It can be used to quickly build a form object in the controller:: .. tip:: - Defining your form as a service is a good practice and make it easily usable in - your application. First, you must tag your form as a form type in the service container: + Defining your form type as a service is a good practice and make it easily usable in + your application : .. configuration-block:: @@ -870,7 +870,7 @@ form "type"). It can be used to quickly build a form object in the controller:: )) ; - That's it! Now you can use your form directly in a controller:: + That's it! Now you can use your form type directly in a controller:: // src/Acme/TaskBundle/Controller/DefaultController.php From 1aa1bde9f5bdb0b1beab40ef39c97f2b319472dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Quatannens?= Date: Mon, 27 May 2013 14:59:38 +0200 Subject: [PATCH 3/4] Fixing typo --- book/forms.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/forms.rst b/book/forms.rst index 4f72a86fe3c..3b2f52aa498 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -835,7 +835,7 @@ form "type"). It can be used to quickly build a form object in the controller:: .. tip:: - Defining your form type as a service is a good practice and make it easily usable in + Defining your form type as a service is a good practice and makes it easily usable in your application : .. configuration-block:: From e134c1e0d0f15a5e09dc393ac82f5bc96d261042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Quatannens?= Date: Tue, 28 May 2013 12:58:40 +0200 Subject: [PATCH 4/4] Following @WouterJ recommandations --- book/forms.rst | 52 +++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 3b2f52aa498..60b809d07cd 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -836,44 +836,43 @@ form "type"). It can be used to quickly build a form object in the controller:: .. tip:: Defining your form type as a service is a good practice and makes it easily usable in - your application : + your application: - .. configuration-block:: + .. configuration-block:: - .. code-block:: yaml + .. code-block:: yaml - # src/Acme/TaskBundle/Resources/config/services.yml - services: - acme_demo.form.type.task: - class: Acme\TaskBundle\Form\Type\TaskType - tags: - - { name: form.type, alias: task } + # src/Acme/TaskBundle/Resources/config/services.yml + services: + acme_demo.form.type.task: + class: Acme\TaskBundle\Form\Type\TaskType + tags: + - { name: form.type, alias: task } - .. code-block:: xml + .. code-block:: xml - - - - + + + + - .. code-block:: php + .. code-block:: php - // src/Acme/TaskBundle/Resources/config/services.php - use Symfony\Component\DependencyInjection\Definition; + // src/Acme/TaskBundle/Resources/config/services.php + use Symfony\Component\DependencyInjection\Definition; - $container - ->setDefinition('acme_demo.form.type.task', new Definition( - 'Acme\TaskBundle\Form\Type\TaskType' - ) - ->addTag('form.type', array( - 'alias' => 'task', - )) - ; + $container + ->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType') + ->addTag('form.type', array( + 'alias' => 'task', + )) + ; That's it! Now you can use your form type directly in a controller:: // src/Acme/TaskBundle/Controller/DefaultController.php + // ... public function newAction() { $task = ...; @@ -897,7 +896,8 @@ form "type"). It can be used to quickly build a form object in the controller:: // ... $builder->add('task', 'task'); - // Note that the property ``task`` (first argument) is defined as a ``task`` form type (second). + // Note that the property ``task`` (first argument) + // is defined as a ``task`` form type (second). } // ...