@@ -91,9 +91,9 @@ from inside a controller::
9191 $task->setDueDate(new \DateTime('tomorrow'));
9292
9393 $form = $this->createFormBuilder($task)
94- ->add('task', 'text ')
95- ->add('dueDate', 'date ')
96- ->add('save', 'submit ', array('label' => 'Create Task'))
94+ ->add('task', 'Symfony\Component\Form\Extension\Core\Type\TextType ')
95+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ')
96+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array('label' => 'Create Task'))
9797 ->getForm();
9898
9999 return $this->render('default/new.html.twig', array(
@@ -116,8 +116,20 @@ building the form.
116116
117117In this example, you've added two fields to your form - ``task `` and ``dueDate `` -
118118corresponding to the ``task `` and ``dueDate `` properties of the ``Task `` class.
119- You've also assigned each a "type" (e.g. ``text ``, ``date ``), which, among
120- other things, determines which HTML form tag(s) is rendered for that field.
119+ You've also assigned each a "type" (e.g. ``TextType `` and ``DateType ``),
120+ represented by its fully qualified class name. Among other things, it determines
121+ which HTML form tag(s) is rendered for that field.
122+
123+ .. tip ::
124+
125+ If you are using PHP 5.5 or later you can use the ``::class `` constant of a
126+ form type to get its fully qualified class name. Make sure you include the
127+ class with a ``use `` statement.
128+
129+ .. versionadded :: 2.8
130+ To denote the form type, you have to use the fully qualified class name.
131+ Before Symfony 2.8, you could use an alias for each type like 'name' or
132+ 'date'.
121133
122134Finally, you added a submit button with a custom label for submitting the form to
123135the server.
@@ -224,9 +236,9 @@ controller::
224236 $task = new Task();
225237
226238 $form = $this->createFormBuilder($task)
227- ->add('task', 'text ')
228- ->add('dueDate', 'date ')
229- ->add('save', 'submit ', array('label' => 'Create Task'))
239+ ->add('task', 'Symfony\Component\Form\Extension\Core\Type\TextType ')
240+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ')
241+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array('label' => 'Create Task'))
230242 ->getForm();
231243
232244 $form->handleRequest($request);
@@ -241,7 +253,7 @@ controller::
241253 'form' => $form->createView(),
242254 ));
243255 }
244-
256+
245257.. caution ::
246258
247259 Be aware that the ``createView() `` method should be called *after * ``handleRequest ``
@@ -310,10 +322,10 @@ which of the buttons was clicked to adapt the program flow in your controller.
310322To do this, add a second button with the caption "Save and add" to your form::
311323
312324 $form = $this->createFormBuilder($task)
313- ->add('task', 'text ')
314- ->add('dueDate', 'date ')
315- ->add('save', 'submit ', array('label' => 'Create Task'))
316- ->add('saveAndAdd', 'submit ', array('label' => 'Save and Add'))
325+ ->add('task', 'Symfony\Component\Form\Extension\Core\Type\TextType ')
326+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ')
327+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array('label' => 'Create Task'))
328+ ->add('saveAndAdd', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array('label' => 'Save and Add'))
317329 ->getForm();
318330
319331In your controller, use the button's
@@ -622,8 +634,8 @@ First, we need to add the two buttons to the form::
622634
623635 $form = $this->createFormBuilder($task)
624636 // ...
625- ->add('nextStep', 'submit ')
626- ->add('previousStep', 'submit ')
637+ ->add('nextStep', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
638+ ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
627639 ->getForm();
628640
629641Then, we configure the button for returning to the previous step to run
@@ -632,7 +644,7 @@ so we set its ``validation_groups`` option to false::
632644
633645 $form = $this->createFormBuilder($task)
634646 // ...
635- ->add('previousStep', 'submit ', array(
647+ ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array(
636648 'validation_groups' => false,
637649 ))
638650 ->getForm();
@@ -701,7 +713,7 @@ the documentation for each type.
701713 The label for the form field can be set using the ``label `` option,
702714 which can be applied to any field::
703715
704- ->add('dueDate', 'date ', array(
716+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ', array(
705717 'widget' => 'single_text',
706718 'label' => 'Due Date',
707719 ))
@@ -722,7 +734,7 @@ Now that you've added validation metadata to the ``Task`` class, Symfony
722734already knows a bit about your fields. If you allow it, Symfony can "guess"
723735the type of your field and set it up for you. In this example, Symfony can
724736guess from the validation rules that both the ``task `` field is a normal
725- ``text `` field and the ``dueDate `` field is a ``date `` field::
737+ ``TextType `` field and the ``dueDate `` field is a ``DateType `` field::
726738
727739 public function newAction()
728740 {
@@ -731,7 +743,7 @@ guess from the validation rules that both the ``task`` field is a normal
731743 $form = $this->createFormBuilder($task)
732744 ->add('task')
733745 ->add('dueDate', null, array('widget' => 'single_text'))
734- ->add('save', 'submit ')
746+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
735747 ->getForm();
736748 }
737749
@@ -992,9 +1004,9 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
9921004 $form = $this->createFormBuilder($task)
9931005 ->setAction($this->generateUrl('target_route'))
9941006 ->setMethod('GET')
995- ->add('task', 'text ')
996- ->add('dueDate', 'date ')
997- ->add('save', 'submit ')
1007+ ->add('task', 'Symfony\Component\Form\Extension\Core\Type\TextType ')
1008+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ')
1009+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
9981010 ->getForm();
9991011
10001012.. note ::
@@ -1064,36 +1076,20 @@ that will house the logic for building the task form::
10641076 $builder
10651077 ->add('task')
10661078 ->add('dueDate', null, array('widget' => 'single_text'))
1067- ->add('save', 'submit ')
1079+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
10681080 ;
10691081 }
1070-
1071- public function getName()
1072- {
1073- return 'task';
1074- }
10751082 }
10761083
1077- .. caution ::
1078-
1079- The ``getName() `` method returns the identifier of this form "type". These
1080- identifiers must be unique in the application. Unless you want to override
1081- a built-in type, they should be different from the default Symfony types
1082- and from any type defined by a third-party bundle installed in your application.
1083- Consider prefixing your types with ``app_ `` to avoid identifier collisions.
1084-
10851084This new class contains all the directions needed to create the task form. It can
10861085be used to quickly build a form object in the controller::
10871086
10881087 // src/AppBundle/Controller/DefaultController.php
10891088
1090- // add this new use statement at the top of the class
1091- use AppBundle\Form\Type\TaskType;
1092-
10931089 public function newAction()
10941090 {
10951091 $task = ...;
1096- $form = $this->createForm(new TaskType() , $task);
1092+ $form = $this->createForm('AppBundle\Form\Type\ TaskType' , $task);
10971093
10981094 // ...
10991095 }
@@ -1160,15 +1156,47 @@ the choice is ultimately up to you.
11601156Defining your Forms as Services
11611157~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11621158
1163- Defining your form type as a service is a good practice and makes it really
1164- easy to use in your application .
1159+ Your form type might have some external dependencies. You can define your form
1160+ type as a service, and inject inject all dependencies you need .
11651161
11661162.. note ::
11671163
11681164 Services and the service container will be handled
11691165 :doc: `later on in this book </book/service_container >`. Things will be
11701166 more clear after reading that chapter.
11711167
1168+ You might want to use a service defined as ``app.my_service `` in your form
1169+ type. Create a constructor to your form type to receive the service::
1170+
1171+ // src/AppBundle/Form/Type/TaskType.php
1172+ namespace AppBundle\Form\Type;
1173+
1174+ use App\Utility\MyService;
1175+ use Symfony\Component\Form\AbstractType;
1176+ use Symfony\Component\Form\FormBuilderInterface;
1177+
1178+ class TaskType extends AbstractType
1179+ {
1180+ private $myService;
1181+
1182+ public function __construct(MyService $mySevice)
1183+ {
1184+ $this->myService = $myService;
1185+ }
1186+
1187+ public function buildForm(FormBuilderInterface $builder, array $options)
1188+ {
1189+ // You can now use myService.
1190+ $builder
1191+ ->add('task')
1192+ ->add('dueDate', null, array('widget' => 'single_text'))
1193+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType')
1194+ ;
1195+ }
1196+ }
1197+
1198+ Define your form type as a service.
1199+
11721200.. configuration-block ::
11731201
11741202 .. code-block :: yaml
@@ -1177,8 +1205,9 @@ easy to use in your application.
11771205 services :
11781206 app.form.type.task :
11791207 class : AppBundle\Form\Type\TaskType
1208+ arguments : ["@app.my_service"]
11801209 tags :
1181- - { name: form.type, alias: task }
1210+ - { name: form.type }
11821211
11831212 .. code-block :: xml
11841213
@@ -1190,52 +1219,25 @@ easy to use in your application.
11901219
11911220 <services >
11921221 <service id =" app.form.type.task" class =" AppBundle\Form\Type\TaskType" >
1193- <tag name = " form.type " alias = " task " / >
1222+ <argument type = " service " id = " app.my_service " ></ argument >
11941223 </service >
11951224 </services >
11961225 </container >
11971226
11981227 .. code-block :: php
11991228
12001229 // src/AppBundle/Resources/config/services.php
1230+ $definition = new Definition('AppBundle\Form\Type\TaskType', array(
1231+ new Reference('app.my_service'),
1232+ ));
12011233 $container
1202- ->register (
1234+ ->setDefinition (
12031235 'app.form.type.task',
1204- 'AppBundle\Form\Type\TaskType'
1236+ $definition
12051237 )
1206- ->addTag('form.type', array(
1207- 'alias' => 'task',
1208- ))
1238+ ->addTag('form.type')
12091239 ;
12101240
1211- That's it! Now you can use your form type directly in a controller::
1212-
1213- // src/AppBundle/Controller/DefaultController.php
1214- // ...
1215-
1216- public function newAction()
1217- {
1218- $task = ...;
1219- $form = $this->createForm('task', $task);
1220-
1221- // ...
1222- }
1223-
1224- or even use from within the form type of another form::
1225-
1226- // src/AppBundle/Form/Type/ListType.php
1227- // ...
1228-
1229- class ListType extends AbstractType
1230- {
1231- public function buildForm(FormBuilderInterface $builder, array $options)
1232- {
1233- // ...
1234-
1235- $builder->add('someTask', 'task');
1236- }
1237- }
1238-
12391241 Read :ref: `form-cookbook-form-field-service ` for more information.
12401242
12411243.. index ::
@@ -1360,11 +1362,6 @@ create a form class so that a ``Category`` object can be modified by the user::
13601362 'data_class' => 'AppBundle\Entity\Category',
13611363 ));
13621364 }
1363-
1364- public function getName()
1365- {
1366- return 'category';
1367- }
13681365 }
13691366
13701367The end goal is to allow the ``Category `` of a ``Task `` to be modified right
@@ -1380,7 +1377,7 @@ class:
13801377 {
13811378 // ...
13821379
1383- $builder->add('category', new CategoryType() );
1380+ $builder->add('category', 'AppBundle\Form\Type\ CategoryType' );
13841381 }
13851382
13861383 The fields from ``CategoryType `` can now be rendered alongside those from
0 commit comments