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

Commit8854b29

Browse files
committed
[symfony#2667] Moving the new section about creating forms as services
1 parenta9d0e3a commit8854b29

File tree

1 file changed

+66
-72
lines changed

1 file changed

+66
-72
lines changed

‎book/forms.rst

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -833,78 +833,6 @@ form "type"). It can be used to quickly build a form object in the controller::
833833
// ...
834834
}
835835

836-
..tip::
837-
838-
Defining your form type as a service is a good practice and makes it easily usable in
839-
your application:
840-
841-
..configuration-block::
842-
843-
..code-block::yaml
844-
845-
# src/Acme/TaskBundle/Resources/config/services.yml
846-
services:
847-
acme_demo.form.type.task:
848-
class:Acme\TaskBundle\Form\Type\TaskType
849-
tags:
850-
-{ name: form.type, alias: task }
851-
852-
..code-block::xml
853-
854-
<!-- src/Acme/TaskBundle/Resources/config/services.xml-->
855-
<serviceid="acme_demo.form.type.task"class="Acme\TaskBundle\Form\Type\TaskType">
856-
<tagname="form.type"alias="task" />
857-
</service>
858-
859-
..code-block::php
860-
861-
// src/Acme/TaskBundle/Resources/config/services.php
862-
use Symfony\Component\DependencyInjection\Definition;
863-
864-
$container
865-
->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
866-
->addTag('form.type', array(
867-
'alias' => 'task',
868-
))
869-
;
870-
871-
That's it! Now you can use your form type directly in a controller::
872-
873-
// src/Acme/TaskBundle/Controller/DefaultController.php
874-
875-
// ...
876-
public function newAction()
877-
{
878-
$task = ...;
879-
$form = $this->createForm('task', $task);
880-
881-
// ...
882-
}
883-
884-
or even use it as a normal type in another form::
885-
886-
// src/Acme/TaskBundle/Form/Type/ListType.php
887-
namespace Acme\TaskBundle\Form\Type;
888-
889-
use Symfony\Component\Form\AbstractType;
890-
use Symfony\Component\Form\FormBuilderInterface;
891-
892-
class ListType extends AbstractType
893-
{
894-
public function buildForm(FormBuilderInterface $builder, array $options)
895-
{
896-
// ...
897-
898-
$builder->add('task', 'task');
899-
// Note that the property ``task`` (first argument)
900-
// is defined as a ``task`` form type (second).
901-
}
902-
903-
// ...
904-
}
905-
906-
Read:ref:`form-cookbook-form-field-service` for more information.
907-
908836
Placing the form logic into its own class means that the form can be easily
909837
reused elsewhere in your project. This is the best way to create forms, but
910838
the choice is ultimately up to you.
@@ -955,6 +883,72 @@ the choice is ultimately up to you.
955883

956884
$form->get('dueDate')->getData();
957885

886+
Defining your Forms as Services
887+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
888+
889+
Defining your form type as a service is a good practice and makes it really
890+
easy to use in your application.
891+
892+
..configuration-block::
893+
894+
..code-block::yaml
895+
896+
# src/Acme/TaskBundle/Resources/config/services.yml
897+
services:
898+
acme_demo.form.type.task:
899+
class:Acme\TaskBundle\Form\Type\TaskType
900+
tags:
901+
-{ name: form.type, alias: task }
902+
903+
..code-block::xml
904+
905+
<!-- src/Acme/TaskBundle/Resources/config/services.xml-->
906+
<serviceid="acme_demo.form.type.task"class="Acme\TaskBundle\Form\Type\TaskType">
907+
<tagname="form.type"alias="task" />
908+
</service>
909+
910+
..code-block::php
911+
912+
// src/Acme/TaskBundle/Resources/config/services.php
913+
use Symfony\Component\DependencyInjection\Definition;
914+
915+
$container
916+
->register('acme_demo.form.type.task', 'Acme\TaskBundle\Form\Type\TaskType')
917+
->addTag('form.type', array(
918+
'alias' => 'task',
919+
))
920+
;
921+
922+
That's it! Now you can use your form type directly in a controller::
923+
924+
// src/Acme/TaskBundle/Controller/DefaultController.php
925+
// ...
926+
927+
public function newAction()
928+
{
929+
$task = ...;
930+
$form = $this->createForm('task', $task);
931+
932+
// ...
933+
}
934+
935+
or even use from within the form type of another form::
936+
937+
// src/Acme/TaskBundle/Form/Type/ListType.php
938+
// ...
939+
940+
class ListType extends AbstractType
941+
{
942+
public function buildForm(FormBuilderInterface $builder, array $options)
943+
{
944+
// ...
945+
946+
$builder->add('someTask', 'task');
947+
}
948+
}
949+
950+
Read:ref:`form-cookbook-form-field-service` for more information.
951+
958952
..index::
959953
pair: Forms; Doctrine
960954

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp