From f9597a622a95d69f7a29ada2f2c2c016b5e6a9a4 Mon Sep 17 00:00:00 2001 From: Peter Bowyer Date: Fri, 12 Feb 2016 15:47:37 +0000 Subject: [PATCH] [DX] Form Types location contradicts Best Practices I wondered why I always put form definitions in `AppBundle/Form/Type`, and whether we were meant to put something else in `AppBundle/Form`. While Googling for an answer I came across http://symfony.com/doc/current/best_practices/forms.html which says the new best practice is to put the form types (which I guess are usually form definitions) directly in `AppBundle/Form`. This PR updates the [main documentation ](http://symfony.com/doc/current/book/forms.html#book-form-creating-form-classes) to reflect the best practices. If anyone can point me to discussions clarifying the difference between form types, form definitions and how the Symfony view has changed (reflected in the change of location) I'd greatly appreciate it. --- book/forms.rst | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 7f527d42216..6a7e472efd6 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1011,6 +1011,9 @@ In :ref:`book-form-creating-form-classes` you will learn how to move the form building code into separate classes. When using an external form class in the controller, you can pass the action and method as form options:: + use AppBundle\Form\TaskType; + // ... + $form = $this->createForm(new TaskType(), $task, array( 'action' => $this->generateUrl('target_route'), 'method' => 'GET', @@ -1056,8 +1059,8 @@ However, a better practice is to build the form in a separate, standalone PHP class, which can then be reused anywhere in your application. Create a new class that will house the logic for building the task form:: - // src/AppBundle/Form/Type/TaskType.php - namespace AppBundle\Form\Type; + // src/AppBundle/Form/TaskType.php + namespace AppBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; @@ -1093,7 +1096,7 @@ be used to quickly build a form object in the controller:: // src/AppBundle/Controller/DefaultController.php // add this new use statement at the top of the class - use AppBundle\Form\Type\TaskType; + use AppBundle\Form\TaskType; public function newAction() { @@ -1181,7 +1184,7 @@ easy to use in your application. # src/AppBundle/Resources/config/services.yml services: app.form.type.task: - class: AppBundle\Form\Type\TaskType + class: AppBundle\Form\TaskType tags: - { name: form.type, alias: app_task } @@ -1194,7 +1197,7 @@ easy to use in your application. xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + @@ -1206,7 +1209,7 @@ easy to use in your application. $container ->register( 'app.form.type.task', - 'AppBundle\Form\Type\TaskType' + 'AppBundle\Form\TaskType' ) ->addTag('form.type', array( 'alias' => 'app_task', @@ -1345,8 +1348,8 @@ Next, add a new ``category`` property to the ``Task`` class:: Now that your application has been updated to reflect the new requirements, create a form class so that a ``Category`` object can be modified by the user:: - // src/AppBundle/Form/Type/CategoryType.php - namespace AppBundle\Form\Type; + // src/AppBundle/Form/CategoryType.php + namespace AppBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; @@ -1375,11 +1378,10 @@ create a form class so that a ``Category`` object can be modified by the user:: The end goal is to allow the ``Category`` of a ``Task`` to be modified right inside the task form itself. To accomplish this, add a ``category`` field to the ``TaskType`` object whose type is an instance of the new ``CategoryType`` -class: - -.. code-block:: php +class:: use Symfony\Component\Form\FormBuilderInterface; + use AppBundle\Form\CategoryType; public function buildForm(FormBuilderInterface $builder, array $options) {