@@ -80,6 +80,9 @@ from inside a controller::
8080 use AppBundle\Entity\Task;
8181 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8282 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;
8386
8487 class DefaultController extends Controller
8588 {
@@ -91,9 +94,11 @@ from inside a controller::
9194 $task->setDueDate(new \DateTime('tomorrow'));
9295
9396 $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'))
97102 ->getForm();
98103
99104 return $this->render('default/new.html.twig', array(
@@ -120,16 +125,10 @@ You've also assigned each a "type" (e.g. ``TextType`` and ``DateType``),
120125represented by its fully qualified class name. Among other things, it determines
121126which HTML form tag(s) is rendered for that field.
122127
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-
129128.. versionadded :: 2.8
130129 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' `` .
133132
134133Finally, you added a submit button with a custom label for submitting the form to
135134the server.
@@ -236,9 +235,9 @@ controller::
236235 $task = new Task();
237236
238237 $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'))
242241 ->getForm();
243242
244243 $form->handleRequest($request);
@@ -322,10 +321,10 @@ which of the buttons was clicked to adapt the program flow in your controller.
322321To do this, add a second button with the caption "Save and add" to your form::
323322
324323 $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'))
329328 ->getForm();
330329
331330In your controller, use the button's
@@ -634,8 +633,8 @@ First, we need to add the two buttons to the form::
634633
635634 $form = $this->createFormBuilder($task)
636635 // ...
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 )
639638 ->getForm();
640639
641640Then, 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::
644643
645644 $form = $this->createFormBuilder($task)
646645 // ...
647- ->add('previousStep', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' , array(
646+ ->add('previousStep', SubmitType::class , array(
648647 'validation_groups' => false,
649648 ))
650649 ->getForm();
@@ -681,7 +680,7 @@ boxes. However, the :doc:`date field </reference/forms/types/date>` can be
681680configured to be rendered as a single text box (where the user would enter
682681the date as a string in the box)::
683682
684- ->add('dueDate', 'date' , array('widget' => 'single_text'))
683+ ->add('dueDate', DateType::class , array('widget' => 'single_text'))
685684
686685.. image :: /images/book/form-simple2.png
687686 :align: center
@@ -713,7 +712,7 @@ the documentation for each type.
713712 The label for the form field can be set using the ``label `` option,
714713 which can be applied to any field::
715714
716- ->add('dueDate', 'Symfony\Component\Form\Extension\Core\Type\ DateType' , array(
715+ ->add('dueDate', DateType::class , array(
717716 'widget' => 'single_text',
718717 'label' => 'Due Date',
719718 ))
@@ -743,7 +742,7 @@ guess from the validation rules that both the ``task`` field is a normal
743742 $form = $this->createFormBuilder($task)
744743 ->add('task')
745744 ->add('dueDate', null, array('widget' => 'single_text'))
746- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
745+ ->add('save', SubmitType::class )
747746 ->getForm();
748747 }
749748
@@ -1004,9 +1003,9 @@ ways. If you build your form in the controller, you can use ``setAction()`` and
10041003 $form = $this->createFormBuilder($task)
10051004 ->setAction($this->generateUrl('target_route'))
10061005 ->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 )
10101009 ->getForm();
10111010
10121011.. note ::
@@ -1068,6 +1067,7 @@ that will house the logic for building the task form::
10681067
10691068 use Symfony\Component\Form\AbstractType;
10701069 use Symfony\Component\Form\FormBuilderInterface;
1070+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10711071
10721072 class TaskType extends AbstractType
10731073 {
@@ -1076,7 +1076,7 @@ that will house the logic for building the task form::
10761076 $builder
10771077 ->add('task')
10781078 ->add('dueDate', null, array('widget' => 'single_text'))
1079- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
1079+ ->add('save', SubmitType::class )
10801080 ;
10811081 }
10821082 }
@@ -1085,11 +1085,12 @@ This new class contains all the directions needed to create the task form. It ca
10851085be used to quickly build a form object in the controller::
10861086
10871087 // src/AppBundle/Controller/DefaultController.php
1088+ use AppBundle\Form\Type\TaskType;
10881089
10891090 public function newAction()
10901091 {
10911092 $task = ...;
1092- $form = $this->createForm('AppBundle\Form\Type\ TaskType' , $task);
1093+ $form = $this->createForm(TaskType::class , $task);
10931094
10941095 // ...
10951096 }
@@ -1136,7 +1137,7 @@ the choice is ultimately up to you.
11361137 $builder
11371138 ->add('task')
11381139 ->add('dueDate', null, array('mapped' => false))
1139- ->add('save', 'submit' )
1140+ ->add('save', SubmitType::class )
11401141 ;
11411142 }
11421143
@@ -1174,6 +1175,7 @@ type. Create a constructor to your form type to receive the service::
11741175 use App\Utility\MyService;
11751176 use Symfony\Component\Form\AbstractType;
11761177 use Symfony\Component\Form\FormBuilderInterface;
1178+ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11771179
11781180 class TaskType extends AbstractType
11791181 {
@@ -1190,7 +1192,7 @@ type. Create a constructor to your form type to receive the service::
11901192 $builder
11911193 ->add('task')
11921194 ->add('dueDate', null, array('widget' => 'single_text'))
1193- ->add('save', 'Symfony\Component\Form\Extension\Core\Type\ SubmitType' )
1195+ ->add('save', SubmitType::class )
11941196 ;
11951197 }
11961198 }
@@ -1228,7 +1230,9 @@ Define your form type as a service.
12281230 .. code-block :: php
12291231
12301232 // 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(
12321236 new Reference('app.my_service'),
12331237 ));
12341238 $container
@@ -1373,12 +1377,13 @@ class:
13731377.. code-block :: php
13741378
13751379 use Symfony\Component\Form\FormBuilderInterface;
1380+ use AppBundle\Form\Type\CategoryType;
13761381
13771382 public function buildForm(FormBuilderInterface $builder, array $options)
13781383 {
13791384 // ...
13801385
1381- $builder->add('category', 'AppBundle\Form\Type\ CategoryType' );
1386+ $builder->add('category', CategoryType::class );
13821387 }
13831388
13841389 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::
18491854 {
18501855 $defaultData = array('message' => 'Type your message here');
18511856 $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 )
18561861 ->getForm();
18571862
18581863 $form->handleRequest($request);
@@ -1913,12 +1918,13 @@ but here's a short example:
19131918
19141919 use Symfony\Component\Validator\Constraints\Length;
19151920 use Symfony\Component\Validator\Constraints\NotBlank;
1921+ use Symfony\Component\Form\Extension\Core\Type\TextType;
19161922
19171923 $builder
1918- ->add('firstName', 'text' , array(
1924+ ->add('firstName', TextType::class , array(
19191925 'constraints' => new Length(array('min' => 3)),
19201926 ))
1921- ->add('lastName', 'text' , array(
1927+ ->add('lastName', TextType::class , array(
19221928 'constraints' => array(
19231929 new NotBlank(),
19241930 new Length(array('min' => 3)),
0 commit comments