Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formtypes should be placed in Form\Type-Directory #5695

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions best_practices/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ 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;
// src/AppBundle/Form/Type/PostType.php
namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
Expand Down Expand Up @@ -51,7 +52,8 @@ form in its own PHP class::

To use the class, use ``createForm`` and instantiate the new class::

use AppBundle\Form\PostType;
// src/AppBundle/Controller/PostController.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this here (it does not really matter how your controller is named and we actually omit most of the class already for readability). Though I would now move the use statement below the placeholder comment.

use AppBundle\Form\Type\PostType;
// ...

public function newAction(Request $request)
Expand Down Expand Up @@ -91,6 +93,7 @@ directly in your form class, this would effectively limit the scope of that form

.. code-block:: php

// src/AppBundle/Form/Type/PostType.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add the namespace declaration and after that add a placeholder comment or add a use statement for the AbstractType class.

class PostType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
Expand All @@ -108,12 +111,13 @@ 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;
use AppBundle\Entity\Post;
use AppBundle\Form\PostType;
use AppBundle\Form\Type\PostType;

class PostController extends Controller
{
Expand All @@ -139,6 +143,7 @@ view layer:

.. code-block:: html+jinja

{# src/AppBundle/Resources/views/Post/new.html.twig #}
{{ form_start(form) }}
{{ form_widget(form) }}

Expand All @@ -159,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) }}
Expand All @@ -176,6 +182,7 @@ Handling a form submit usually follows a similar template:

.. code-block:: php

// src/AppBundle/Controller/PostController.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove it here too (for the same reasons as above).

public function newAction(Request $request)
{
// build the form ...
Expand Down