@@ -80,6 +80,9 @@ from inside a controller::
80
80
use AppBundle\Entity\Task;
81
81
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
82
82
use Symfony\Component\HttpFoundation\Request;
83
+ use Symfony\Component\Form\Extension\Core\Type\TextType;
84
+ use Symfony\Component\Form\Extension\Core\Type\DateType;
85
+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
83
86
84
87
class DefaultController extends Controller
85
88
{
@@ -91,9 +94,11 @@ from inside a controller::
91
94
$task->setDueDate(new \DateTime('tomorrow'));
92
95
93
96
$form = $this->createFormBuilder($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
+ ->add('task', TextType::class)
98
+ // If you use PHP 5.3 or 5.4 you must use
99
+ // ->add('task', 'Symfony\Component\Form\Extension\Core\Type\TextType')
100
+ ->add('dueDate', DateType::class)
101
+ ->add('save', SubmitType::class, array('label' => 'Create Task'))
97
102
->getForm();
98
103
99
104
return $this->render('default/new.html.twig', array(
@@ -120,16 +125,10 @@ You've also assigned each a "type" (e.g. ``TextType`` and ``DateType``),
120
125
represented by its fully qualified class name. Among other things, it determines
121
126
which HTML form tag(s) is rendered for that field.
122
127
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
128
.. versionadded :: 2.8
130
129
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'.
130
+ Before Symfony 2.8, you could use an alias for each type like `` 'name' `` or
131
+ `` 'date' `` .
133
132
134
133
Finally, you added a submit button with a custom label for submitting the form to
135
134
the server.
@@ -236,9 +235,9 @@ controller::
236
235
$task = new Task();
237
236
238
237
$form = $this->createFormBuilder($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'))
238
+ ->add('task', TextType::class )
239
+ ->add('dueDate', DateType::class )
240
+ ->add('save', SubmitType::class , array('label' => 'Create Task'))
242
241
->getForm();
243
242
244
243
$form->handleRequest($request);
@@ -322,10 +321,10 @@ which of the buttons was clicked to adapt the program flow in your controller.
322
321
To do this, add a second button with the caption "Save and add" to your form::
323
322
324
323
$form = $this->createFormBuilder($task)
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'))
324
+ ->add('task', TextType::class )
325
+ ->add('dueDate', DateType::class )
326
+ ->add('save', SubmitType::class , array('label' => 'Create Task'))
327
+ ->add('saveAndAdd', SubmitType::class , array('label' => 'Save and Add'))
329
328
->getForm();
330
329
331
330
In your controller, use the button's
@@ -634,8 +633,8 @@ First, we need to add the two buttons to the form::
634
633
635
634
$form = $this->createFormBuilder($task)
636
635
// ...
637
- ->add('nextStep', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
638
- ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
636
+ ->add('nextStep', SubmitType::class )
637
+ ->add('previousStep', SubmitType::class )
639
638
->getForm();
640
639
641
640
Then, we configure the button for returning to the previous step to run
@@ -644,7 +643,7 @@ so we set its ``validation_groups`` option to false::
644
643
645
644
$form = $this->createFormBuilder($task)
646
645
// ...
647
- ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' , array(
646
+ ->add('previousStep', SubmitType::class , array(
648
647
'validation_groups' => false,
649
648
))
650
649
->getForm();
@@ -681,7 +680,7 @@ boxes. However, the :doc:`date field </reference/forms/types/date>` can be
681
680
configured to be rendered as a single text box (where the user would enter
682
681
the date as a string in the box)::
683
682
684
- ->add('dueDate', 'date' , array('widget' => 'single_text'))
683
+ ->add('dueDate', DateType::class , array('widget' => 'single_text'))
685
684
686
685
.. image :: /images/book/form-simple2.png
687
686
:align: center
@@ -713,7 +712,7 @@ the documentation for each type.
713
712
The label for the form field can be set using the ``label `` option,
714
713
which can be applied to any field::
715
714
716
- ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\ DateType' , array(
715
+ ->add('dueDate', DateType::class , array(
717
716
'widget' => 'single_text',
718
717
'label' => 'Due Date',
719
718
))
@@ -743,7 +742,7 @@ guess from the validation rules that both the ``task`` field is a normal
743
742
$form = $this->createFormBuilder($task)
744
743
->add('task')
745
744
->add('dueDate', null, array('widget' => 'single_text'))
746
- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
745
+ ->add('save', SubmitType::class )
747
746
->getForm();
748
747
}
749
748
@@ -1004,9 +1003,9 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
1004
1003
$form = $this->createFormBuilder($task)
1005
1004
->setAction($this->generateUrl('target_route'))
1006
1005
->setMethod('GET')
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' )
1006
+ ->add('task', TextType::class )
1007
+ ->add('dueDate', DateType::clas )
1008
+ ->add('save', SubmitType::class )
1010
1009
->getForm();
1011
1010
1012
1011
.. note ::
@@ -1068,6 +1067,7 @@ that will house the logic for building the task form::
1068
1067
1069
1068
use Symfony\Component\Form\AbstractType;
1070
1069
use Symfony\Component\Form\FormBuilderInterface;
1070
+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1071
1071
1072
1072
class TaskType extends AbstractType
1073
1073
{
@@ -1076,7 +1076,7 @@ that will house the logic for building the task form::
1076
1076
$builder
1077
1077
->add('task')
1078
1078
->add('dueDate', null, array('widget' => 'single_text'))
1079
- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
1079
+ ->add('save', SubmitType::class )
1080
1080
;
1081
1081
}
1082
1082
}
@@ -1085,11 +1085,12 @@ This new class contains all the directions needed to create the task form. It ca
1085
1085
be used to quickly build a form object in the controller::
1086
1086
1087
1087
// src/AppBundle/Controller/DefaultController.php
1088
+ use AppBundle\Form\Type\TaskType;
1088
1089
1089
1090
public function newAction()
1090
1091
{
1091
1092
$task = ...;
1092
- $form = $this->createForm('AppBundle\Form\Type\ TaskType' , $task);
1093
+ $form = $this->createForm(TaskType::class , $task);
1093
1094
1094
1095
// ...
1095
1096
}
@@ -1136,7 +1137,7 @@ the choice is ultimately up to you.
1136
1137
$builder
1137
1138
->add('task')
1138
1139
->add('dueDate', null, array('mapped' => false))
1139
- ->add('save', 'submit' )
1140
+ ->add('save', SubmitType::class )
1140
1141
;
1141
1142
}
1142
1143
@@ -1174,6 +1175,7 @@ type. Create a constructor to your form type to receive the service::
1174
1175
use App\Utility\MyService;
1175
1176
use Symfony\Component\Form\AbstractType;
1176
1177
use Symfony\Component\Form\FormBuilderInterface;
1178
+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1177
1179
1178
1180
class TaskType extends AbstractType
1179
1181
{
@@ -1190,7 +1192,7 @@ type. Create a constructor to your form type to receive the service::
1190
1192
$builder
1191
1193
->add('task')
1192
1194
->add('dueDate', null, array('widget' => 'single_text'))
1193
- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
1195
+ ->add('save', SubmitType::class )
1194
1196
;
1195
1197
}
1196
1198
}
@@ -1228,7 +1230,9 @@ Define your form type as a service.
1228
1230
.. code-block :: php
1229
1231
1230
1232
// src/AppBundle/Resources/config/services.php
1231
- $definition = new Definition('AppBundle\Form\Type\TaskType', array(
1233
+ use AppBundle\Form\Type\TaskType;
1234
+
1235
+ $definition = new Definition(TaskType::class, array(
1232
1236
new Reference('app.my_service'),
1233
1237
));
1234
1238
$container
@@ -1373,12 +1377,13 @@ class:
1373
1377
.. code-block :: php
1374
1378
1375
1379
use Symfony\Component\Form\FormBuilderInterface;
1380
+ use AppBundle\Form\Type\CategoryType;
1376
1381
1377
1382
public function buildForm(FormBuilderInterface $builder, array $options)
1378
1383
{
1379
1384
// ...
1380
1385
1381
- $builder->add('category', 'AppBundle\Form\Type\ CategoryType' );
1386
+ $builder->add('category', CategoryType::class );
1382
1387
}
1383
1388
1384
1389
The fields from ``CategoryType `` can now be rendered alongside those from
@@ -1849,10 +1854,10 @@ an array of the submitted data. This is actually really easy::
1849
1854
{
1850
1855
$defaultData = array('message' => 'Type your message here');
1851
1856
$form = $this->createFormBuilder($defaultData)
1852
- ->add('name', 'text' )
1853
- ->add('email', 'email' )
1854
- ->add('message', 'textarea' )
1855
- ->add('send', 'submit' )
1857
+ ->add('name', TextType::class )
1858
+ ->add('email', EmailType::class )
1859
+ ->add('message', TextareaType::class )
1860
+ ->add('send', SubmitType::class )
1856
1861
->getForm();
1857
1862
1858
1863
$form->handleRequest($request);
@@ -1913,12 +1918,13 @@ but here's a short example:
1913
1918
1914
1919
use Symfony\Component\Validator\Constraints\Length;
1915
1920
use Symfony\Component\Validator\Constraints\NotBlank;
1921
+ use Symfony\Component\Form\Extension\Core\Type\TextType;
1916
1922
1917
1923
$builder
1918
- ->add('firstName', 'text' , array(
1924
+ ->add('firstName', TextType::class , array(
1919
1925
'constraints' => new Length(array('min' => 3)),
1920
1926
))
1921
- ->add('lastName', 'text' , array(
1927
+ ->add('lastName', TextType::class , array(
1922
1928
'constraints' => array(
1923
1929
new NotBlank(),
1924
1930
new Length(array('min' => 3)),
0 commit comments