-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Book/Form : Adding a new section about defining forms as service #2667
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
Changes from 3 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 |
---|---|---|
|
@@ -833,6 +833,78 @@ 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 : | ||
|
||
.. configuration-block:: | ||
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. this directive needs to be indented on the same level as the text |
||
|
||
.. 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 | ||
|
||
<!-- src/Acme/TaskBundle/Resources/config/services.xml --> | ||
<service id="acme_demo.form.type.task" class="Acme\TaskBundle\Form\Type\TaskType"> | ||
<tag name="form.type" alias="task" /> | ||
</service> | ||
|
||
.. 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' | ||
) | ||
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 prefer |
||
->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 | ||
|
||
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. you should remove this line 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. no, actually it should be: // src/Acme/TaskBundle/Controller/DefaultController.php
// ...
public function newAction() |
||
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). | ||
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 think you need to wrap this in 2 lines (max. 85 characters in code blocks) |
||
} | ||
|
||
// ... | ||
} | ||
|
||
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. | ||
|
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.
you should remove the space before the colon