From 19798cf9390218dbde7f39d1b37fd9fee0bd3838 Mon Sep 17 00:00:00 2001 From: Hannes Kandulla Date: Tue, 15 Sep 2015 10:18:52 +0200 Subject: [PATCH 1/3] Formtypes should be placed in Form\Type-Directory --- best_practices/forms.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/best_practices/forms.rst b/best_practices/forms.rst index 12255f37cfc..ee18640f8ae 100644 --- a/best_practices/forms.rst +++ b/best_practices/forms.rst @@ -17,7 +17,7 @@ code. This is perfectly fine if you don't need to reuse the form somewhere else. But for organization and reuse, we recommend that you define each form in its own PHP class:: - namespace AppBundle\Form; + namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; @@ -51,7 +51,7 @@ form in its own PHP class:: To use the class, use ``createForm`` and instantiate the new class:: - use AppBundle\Form\PostType; + use AppBundle\Form\Type\PostType; // ... public function newAction(Request $request) @@ -113,7 +113,7 @@ some developers configure form buttons in the controller:: use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use AppBundle\Entity\Post; - use AppBundle\Form\PostType; + use AppBundle\Form\Type\PostType; class PostController extends Controller { From c7a3f259b957f7efe7e58c6599e8944586890ded Mon Sep 17 00:00:00 2001 From: Hannes Kandulla Date: Mon, 28 Sep 2015 10:10:49 +0200 Subject: [PATCH 2/3] Included hint about file location --- best_practices/forms.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/best_practices/forms.rst b/best_practices/forms.rst index ee18640f8ae..87ebb632ce3 100644 --- a/best_practices/forms.rst +++ b/best_practices/forms.rst @@ -17,6 +17,7 @@ code. This is perfectly fine if you don't need to reuse the form somewhere else. But for organization and reuse, we recommend that you define each form in its own PHP class:: + // src/AppBundle/Form/Type/PostType.php namespace AppBundle\Form\Type; use Symfony\Component\Form\AbstractType; @@ -91,6 +92,7 @@ directly in your form class, this would effectively limit the scope of that form .. code-block:: php + // src/AppBundle/Form/Type/PostType.php class PostType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) From 08d9814e369e16a87ba02d74792747bc650e26ad Mon Sep 17 00:00:00 2001 From: Hannes Kandulla Date: Mon, 28 Sep 2015 10:22:51 +0200 Subject: [PATCH 3/3] Included hint about file location Included hint about file location for Controller- and View-Files. --- best_practices/forms.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/best_practices/forms.rst b/best_practices/forms.rst index 87ebb632ce3..e51374c7363 100644 --- a/best_practices/forms.rst +++ b/best_practices/forms.rst @@ -52,6 +52,7 @@ form in its own PHP class:: To use the class, use ``createForm`` and instantiate the new class:: + // src/AppBundle/Controller/PostController.php use AppBundle\Form\Type\PostType; // ... @@ -110,7 +111,8 @@ This form *may* have been designed for creating posts, but if you wanted to reuse it for editing posts, the button label would be wrong. Instead, some developers configure form buttons in the controller:: - namespace AppBundle\Controller\Admin; + // src/AppBundle/Controller/PostController.php + namespace AppBundle\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -141,6 +143,7 @@ view layer: .. code-block:: html+jinja + {# src/AppBundle/Resources/views/Post/new.html.twig #} {{ form_start(form) }} {{ form_widget(form) }} @@ -161,6 +164,7 @@ fields: .. code-block:: html+jinja + {# src/AppBundle/Resources/views/Post/new.html.twig #} {{ form_start(form, {'attr': {'class': 'my-form-class'} }) }} {{ form_widget(form) }} {{ form_end(form) }} @@ -178,6 +182,7 @@ Handling a form submit usually follows a similar template: .. code-block:: php + // src/AppBundle/Controller/PostController.php public function newAction(Request $request) { // build the form ...