@@ -91,9 +91,9 @@ from inside a controller::
91
91
$task->setDueDate(new \DateTime('tomorrow'));
92
92
93
93
$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'))
97
97
->getForm();
98
98
99
99
return $this->render('default/new.html.twig', array(
@@ -116,8 +116,20 @@ building the form.
116
116
117
117
In this example, you've added two fields to your form - ``task `` and ``dueDate `` -
118
118
corresponding 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'.
121
133
122
134
Finally, you added a submit button with a custom label for submitting the form to
123
135
the server.
@@ -224,9 +236,9 @@ controller::
224
236
$task = new Task();
225
237
226
238
$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'))
230
242
->getForm();
231
243
232
244
$form->handleRequest($request);
@@ -241,7 +253,7 @@ controller::
241
253
'form' => $form->createView(),
242
254
));
243
255
}
244
-
256
+
245
257
.. caution ::
246
258
247
259
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.
310
322
To do this, add a second button with the caption "Save and add" to your form::
311
323
312
324
$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'))
317
329
->getForm();
318
330
319
331
In your controller, use the button's
@@ -622,8 +634,8 @@ First, we need to add the two buttons to the form::
622
634
623
635
$form = $this->createFormBuilder($task)
624
636
// ...
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 ')
627
639
->getForm();
628
640
629
641
Then, 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::
632
644
633
645
$form = $this->createFormBuilder($task)
634
646
// ...
635
- ->add('previousStep', 'submit ', array(
647
+ ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ', array(
636
648
'validation_groups' => false,
637
649
))
638
650
->getForm();
@@ -701,7 +713,7 @@ the documentation for each type.
701
713
The label for the form field can be set using the ``label `` option,
702
714
which can be applied to any field::
703
715
704
- ->add('dueDate', 'date ', array(
716
+ ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\DateType ', array(
705
717
'widget' => 'single_text',
706
718
'label' => 'Due Date',
707
719
))
@@ -722,7 +734,7 @@ Now that you've added validation metadata to the ``Task`` class, Symfony
722
734
already knows a bit about your fields. If you allow it, Symfony can "guess"
723
735
the type of your field and set it up for you. In this example, Symfony can
724
736
guess 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::
726
738
727
739
public function newAction()
728
740
{
@@ -731,7 +743,7 @@ guess from the validation rules that both the ``task`` field is a normal
731
743
$form = $this->createFormBuilder($task)
732
744
->add('task')
733
745
->add('dueDate', null, array('widget' => 'single_text'))
734
- ->add('save', 'submit ')
746
+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
735
747
->getForm();
736
748
}
737
749
@@ -992,9 +1004,9 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
992
1004
$form = $this->createFormBuilder($task)
993
1005
->setAction($this->generateUrl('target_route'))
994
1006
->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 ')
998
1010
->getForm();
999
1011
1000
1012
.. note ::
@@ -1064,36 +1076,20 @@ that will house the logic for building the task form::
1064
1076
$builder
1065
1077
->add('task')
1066
1078
->add('dueDate', null, array('widget' => 'single_text'))
1067
- ->add('save', 'submit ')
1079
+ ->add('save', 'Symfony\Component\Form\Extension\Core\Type\SubmitType ')
1068
1080
;
1069
1081
}
1070
-
1071
- public function getName()
1072
- {
1073
- return 'task';
1074
- }
1075
1082
}
1076
1083
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
-
1085
1084
This new class contains all the directions needed to create the task form. It can
1086
1085
be used to quickly build a form object in the controller::
1087
1086
1088
1087
// src/AppBundle/Controller/DefaultController.php
1089
1088
1090
- // add this new use statement at the top of the class
1091
- use AppBundle\Form\Type\TaskType;
1092
-
1093
1089
public function newAction()
1094
1090
{
1095
1091
$task = ...;
1096
- $form = $this->createForm(new TaskType() , $task);
1092
+ $form = $this->createForm('AppBundle\Form\Type\ TaskType' , $task);
1097
1093
1098
1094
// ...
1099
1095
}
@@ -1160,15 +1156,47 @@ the choice is ultimately up to you.
1160
1156
Defining your Forms as Services
1161
1157
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1162
1158
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 .
1165
1161
1166
1162
.. note ::
1167
1163
1168
1164
Services and the service container will be handled
1169
1165
:doc: `later on in this book </book/service_container >`. Things will be
1170
1166
more clear after reading that chapter.
1171
1167
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
+
1172
1200
.. configuration-block ::
1173
1201
1174
1202
.. code-block :: yaml
@@ -1177,8 +1205,9 @@ easy to use in your application.
1177
1205
services :
1178
1206
app.form.type.task :
1179
1207
class : AppBundle\Form\Type\TaskType
1208
+ arguments : ["@app.my_service"]
1180
1209
tags :
1181
- - { name: form.type, alias: task }
1210
+ - { name: form.type }
1182
1211
1183
1212
.. code-block :: xml
1184
1213
@@ -1190,52 +1219,25 @@ easy to use in your application.
1190
1219
1191
1220
<services >
1192
1221
<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 >
1194
1223
</service >
1195
1224
</services >
1196
1225
</container >
1197
1226
1198
1227
.. code-block :: php
1199
1228
1200
1229
// src/AppBundle/Resources/config/services.php
1230
+ $definition = new Definition('AppBundle\Form\Type\TaskType', array(
1231
+ new Reference('app.my_service'),
1232
+ ));
1201
1233
$container
1202
- ->register (
1234
+ ->setDefinition (
1203
1235
'app.form.type.task',
1204
- 'AppBundle\Form\Type\TaskType'
1236
+ $definition
1205
1237
)
1206
- ->addTag('form.type', array(
1207
- 'alias' => 'task',
1208
- ))
1238
+ ->addTag('form.type')
1209
1239
;
1210
1240
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
-
1239
1241
Read :ref: `form-cookbook-form-field-service ` for more information.
1240
1242
1241
1243
.. index ::
@@ -1360,11 +1362,6 @@ create a form class so that a ``Category`` object can be modified by the user::
1360
1362
'data_class' => 'AppBundle\Entity\Category',
1361
1363
));
1362
1364
}
1363
-
1364
- public function getName()
1365
- {
1366
- return 'category';
1367
- }
1368
1365
}
1369
1366
1370
1367
The end goal is to allow the ``Category `` of a ``Task `` to be modified right
@@ -1380,7 +1377,7 @@ class:
1380
1377
{
1381
1378
// ...
1382
1379
1383
- $builder->add('category', new CategoryType() );
1380
+ $builder->add('category', 'AppBundle\Form\Type\ CategoryType' );
1384
1381
}
1385
1382
1386
1383
The fields from ``CategoryType `` can now be rendered alongside those from
0 commit comments