-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
use AppBundle\Form\Type\PostType; | ||
// ... | ||
|
||
public function newAction(Request $request) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add the |
||
class PostType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
|
@@ -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 | ||
{ | ||
|
@@ -139,6 +143,7 @@ view layer: | |
|
||
.. code-block:: html+jinja | ||
|
||
{# src/AppBundle/Resources/views/Post/new.html.twig #} | ||
{{ form_start(form) }} | ||
{{ form_widget(form) }} | ||
|
||
|
@@ -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) }} | ||
|
@@ -176,6 +182,7 @@ Handling a form submit usually follows a similar template: | |
|
||
.. code-block:: php | ||
|
||
// src/AppBundle/Controller/PostController.php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ... | ||
|
There was a problem hiding this comment.
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.