Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 4 commits intosymfony:2.1fromgregquat:patch-15
Jun 4, 2013
Merged
Changes fromall 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 deletionsbook/forms.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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:

.. configuration-block::

.. 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
->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
->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// ...publicfunction 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).
}

// ...
}

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

[8]ページ先頭

©2009-2025 Movatter.jp