@@ -25,6 +25,7 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
25
25
26
26
use Symfony\Component\Form\AbstractType;
27
27
use Symfony\Component\OptionsResolver\OptionsResolver;
28
+ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
28
29
29
30
class GenderType extends AbstractType
30
31
{
@@ -40,12 +41,7 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
40
41
41
42
public function getParent()
42
43
{
43
- return 'choice';
44
- }
45
-
46
- public function getName()
47
- {
48
- return 'gender';
44
+ return ChoiceType::class;
49
45
}
50
46
}
51
47
@@ -54,8 +50,12 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
54
50
The location of this file is not important - the ``Form\Type `` directory
55
51
is just a convention.
56
52
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
+
57
57
Here, 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
59
59
all of the logic and rendering of that field type. To see some of the logic,
60
60
check out the `ChoiceType `_ class. There are three methods that are particularly
61
61
important:
@@ -89,24 +89,26 @@ important:
89
89
Also, if you need to modify the "view" of any of your child types from
90
90
your parent type, use the ``finishView() `` method.
91
91
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
-
96
92
The goal of this field was to extend the choice type to enable selection of
97
93
a gender. This is achieved by fixing the ``choices `` to a list of possible
98
94
genders.
99
95
100
96
Creating a Template for the Field
101
97
---------------------------------
102
98
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
105
101
:ref: `cookbook-form-customization-form-themes `.
106
102
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"
110
112
(i.e. radio buttons or checkboxes, instead of a select field), you want to
111
113
always render it in a ``ul `` element. In your form theme template (see above
112
114
link 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:
154
156
.. note ::
155
157
156
158
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 `) .
158
160
Further, the main config file should point to the custom form template
159
161
so that it's used when rendering all forms.
160
162
@@ -241,18 +243,19 @@ new instance of the type in one of your forms::
241
243
242
244
use Symfony\Component\Form\AbstractType;
243
245
use Symfony\Component\Form\FormBuilderInterface;
246
+ use AppBundle\Form\Type\GenderType;
244
247
245
248
class AuthorType extends AbstractType
246
249
{
247
250
public function buildForm(FormBuilderInterface $builder, array $options)
248
251
{
249
- $builder->add('gender_code', new GenderType() , array(
252
+ $builder->add('gender_code', GenderType::class , array(
250
253
'placeholder' => 'Choose a gender',
251
254
));
252
255
}
253
256
}
254
257
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
256
259
the gender codes were stored in configuration or in a database? The next
257
260
section explains how more complex field types solve this problem.
258
261
@@ -311,14 +314,14 @@ the ``genders`` parameter value as the first argument to its to-be-created
311
314
arguments :
312
315
- " %genders%"
313
316
tags :
314
- - { name: form.type, alias: gender }
317
+ - { name: form.type }
315
318
316
319
.. code-block :: xml
317
320
318
321
<!-- src/AppBundle/Resources/config/services.xml -->
319
322
<service id =" app.form.type.gender" class =" AppBundle\Form\Type\GenderType" >
320
323
<argument >%genders%</argument >
321
- <tag name =" form.type" alias = " gender " />
324
+ <tag name =" form.type" />
322
325
</service >
323
326
324
327
.. code-block :: php
@@ -331,20 +334,16 @@ the ``genders`` parameter value as the first argument to its to-be-created
331
334
'AppBundle\Form\Type\GenderType',
332
335
array('%genders%')
333
336
))
334
- ->addTag('form.type', array(
335
- 'alias' => 'gender',
336
- ))
337
+ ->addTag('form.type')
337
338
;
338
339
339
340
.. tip ::
340
341
341
342
Make sure the services file is being imported. See :ref: `service-container-imports-directive `
342
343
for details.
343
344
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::
348
347
349
348
// src/AppBundle/Form/Type/GenderType.php
350
349
namespace AppBundle\Form\Type;
@@ -374,28 +373,28 @@ method to ``GenderType``, which receives the gender configuration::
374
373
}
375
374
376
375
Great! 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::
379
379
380
380
// src/AppBundle/Form/Type/AuthorType.php
381
381
namespace AppBundle\Form\Type;
382
382
383
+ use Symfony\Component\Form\AbstractType;
383
384
use Symfony\Component\Form\FormBuilderInterface;
384
-
385
- // ...
385
+ use AppBundle\Form\Type\GenderType;
386
386
387
387
class AuthorType extends AbstractType
388
388
{
389
389
public function buildForm(FormBuilderInterface $builder, array $options)
390
390
{
391
- $builder->add('gender_code', 'gender' , array(
391
+ $builder->add('gender_code', GenderType::class , array(
392
392
'placeholder' => 'Choose a gender',
393
393
));
394
394
}
395
395
}
396
396
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!
399
398
400
399
.. _`ChoiceType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
401
400
.. _`FieldType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
0 commit comments