Skip to content

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

Merged
merged 4 commits into from
Jun 4, 2013
Merged
Changes from 3 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
72 changes: 72 additions & 0 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Copy link
Member

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


.. configuration-block::
Copy link
Member

Choose a reason for hiding this comment

The 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'
)
Copy link
Member

Choose a reason for hiding this comment

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

I prefer ->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType)' for simple services as this one

->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

Copy link
Member

Choose a reason for hiding this comment

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

you should remove this line

Copy link
Member

Choose a reason for hiding this comment

The 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).
Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand Down