@@ -25,6 +25,7 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
2525
2626 use Symfony\Component\Form\AbstractType;
2727 use Symfony\Component\OptionsResolver\OptionsResolver;
28+ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
2829
2930 class GenderType extends AbstractType
3031 {
@@ -40,12 +41,7 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
4041
4142 public function getParent()
4243 {
43- return 'choice';
44- }
45-
46- public function getName()
47- {
48- return 'gender';
44+ return ChoiceType::class;
4945 }
5046 }
5147
@@ -54,8 +50,12 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
5450 The location of this file is not important - the ``Form\Type `` directory
5551 is just a convention.
5652
53+ .. versionadded :: 2.8
54+ In 2.8, the ``getName() `` method was removed. Now, fields are always referred
55+ to by their fully-qualified class name.
56+
5757Here, the return value of the ``getParent `` function indicates that you're
58- extending the ``choice `` field type . This means that, by default, you inherit
58+ extending the ``ChoiceType `` field. This means that, by default, you inherit
5959all of the logic and rendering of that field type. To see some of the logic,
6060check out the `ChoiceType `_ class. There are three methods that are particularly
6161important:
@@ -89,24 +89,26 @@ important:
8989 Also, if you need to modify the "view" of any of your child types from
9090 your parent type, use the ``finishView() `` method.
9191
92- The ``getName() `` method returns an identifier which should be unique in
93- your application. This is used in various places, such as when customizing
94- how your form type will be rendered.
95-
9692The goal of this field was to extend the choice type to enable selection of
9793a gender. This is achieved by fixing the ``choices `` to a list of possible
9894genders.
9995
10096Creating a Template for the Field
10197---------------------------------
10298
103- Each field type is rendered by a template fragment, which is determined in
104- part by the value of your `` getName() `` method . For more information, see
99+ Each field type is rendered by a template fragment, which is determined in part by
100+ the class name of your type . For more information, see
105101:ref: `cookbook-form-customization-form-themes `.
106102
107- In this case, since the parent field is ``choice ``, you don't *need * to do
108- any work as the custom field type will automatically be rendered like a ``choice ``
109- type. But for the sake of this example, suppose that when your field is "expanded"
103+ .. note ::
104+
105+ The first part of the prefix (e.g. ``gender ``) comes from the class name
106+ (``GenderType `` -> ``gender ``). This can be controlled by overriding ``getBlockPrefix() ``
107+ in ``GenderType ``.
108+
109+ In this case, since the parent field is ``ChoiceType ``, you don't *need * to do
110+ any work as the custom field type will automatically be rendered like a ``ChoiceType ``.
111+ But for the sake of this example, suppose that when your field is "expanded"
110112(i.e. radio buttons or checkboxes, instead of a select field), you want to
111113always render it in a ``ul `` element. In your form theme template (see above
112114link for details), create a ``gender_widget `` block to handle this:
@@ -154,7 +156,7 @@ link for details), create a ``gender_widget`` block to handle this:
154156.. note ::
155157
156158 Make sure the correct widget prefix is used. In this example the name should
157- be ``gender_widget ``, according to the value returned by `` getName `` .
159+ be ``gender_widget `` (see :ref: ` cookbook-form-customization-form-themes `) .
158160 Further, the main config file should point to the custom form template
159161 so that it's used when rendering all forms.
160162
@@ -241,18 +243,19 @@ new instance of the type in one of your forms::
241243
242244 use Symfony\Component\Form\AbstractType;
243245 use Symfony\Component\Form\FormBuilderInterface;
246+ use AppBundle\Form\Type\GenderType;
244247
245248 class AuthorType extends AbstractType
246249 {
247250 public function buildForm(FormBuilderInterface $builder, array $options)
248251 {
249- $builder->add('gender_code', new GenderType() , array(
252+ $builder->add('gender_code', GenderType::class , array(
250253 'placeholder' => 'Choose a gender',
251254 ));
252255 }
253256 }
254257
255- But this only works because the ``GenderType() `` is very simple. What if
258+ But this only works because the ``GenderType `` is very simple. What if
256259the gender codes were stored in configuration or in a database? The next
257260section explains how more complex field types solve this problem.
258261
@@ -311,14 +314,14 @@ the ``genders`` parameter value as the first argument to its to-be-created
311314 arguments :
312315 - " %genders%"
313316 tags :
314- - { name: form.type, alias: gender }
317+ - { name: form.type }
315318
316319 .. code-block :: xml
317320
318321 <!-- src/AppBundle/Resources/config/services.xml -->
319322 <service id =" app.form.type.gender" class =" AppBundle\Form\Type\GenderType" >
320323 <argument >%genders%</argument >
321- <tag name =" form.type" alias = " gender " />
324+ <tag name =" form.type" />
322325 </service >
323326
324327 .. code-block :: php
@@ -331,20 +334,16 @@ the ``genders`` parameter value as the first argument to its to-be-created
331334 'AppBundle\Form\Type\GenderType',
332335 array('%genders%')
333336 ))
334- ->addTag('form.type', array(
335- 'alias' => 'gender',
336- ))
337+ ->addTag('form.type')
337338 ;
338339
339340 .. tip ::
340341
341342 Make sure the services file is being imported. See :ref: `service-container-imports-directive `
342343 for details.
343344
344- Be sure that the ``alias `` attribute of the tag corresponds with the value
345- returned by the ``getName `` method defined earlier. You'll see the importance
346- of this in a moment when you use the custom field type. But first, add a ``__construct ``
347- method to ``GenderType ``, which receives the gender configuration::
345+ First, add a ``__construct `` method to ``GenderType ``, which receives the gender
346+ configuration::
348347
349348 // src/AppBundle/Form/Type/GenderType.php
350349 namespace AppBundle\Form\Type;
@@ -374,28 +373,28 @@ method to ``GenderType``, which receives the gender configuration::
374373 }
375374
376375Great! The ``GenderType `` is now fueled by the configuration parameters and
377- registered as a service. Additionally, because you used the ``form.type `` alias in its
378- configuration, using the field is now much easier::
376+ registered as a service. Because you used the ``form.type `` alias in its configuration,
377+ your service will be used instead of creating a *new * ``GenderType ``. In other words,
378+ your controller *does not need to change *, it still looks like this::
379379
380380 // src/AppBundle/Form/Type/AuthorType.php
381381 namespace AppBundle\Form\Type;
382382
383+ use Symfony\Component\Form\AbstractType;
383384 use Symfony\Component\Form\FormBuilderInterface;
384-
385- // ...
385+ use AppBundle\Form\Type\GenderType;
386386
387387 class AuthorType extends AbstractType
388388 {
389389 public function buildForm(FormBuilderInterface $builder, array $options)
390390 {
391- $builder->add('gender_code', 'gender' , array(
391+ $builder->add('gender_code', GenderType::class , array(
392392 'placeholder' => 'Choose a gender',
393393 ));
394394 }
395395 }
396396
397- Notice that instead of instantiating a new instance, you can just refer to
398- it by the alias used in your service configuration, ``gender ``. Have fun!
397+ Have fun!
399398
400399.. _`ChoiceType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
401400.. _`FieldType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
0 commit comments