@@ -5,17 +5,17 @@ How to Create a Custom Form Field Type
5
5
======================================
6
6
7
7
Symfony comes with a bunch of core field types available for building forms.
8
- However there are situations where we want to create a custom form field
9
- type for a specific purpose. This recipe assumes we need a field definition
8
+ However there are situations where you may want to create a custom form field
9
+ type for a specific purpose. This recipe assumes you need a field definition
10
10
that holds a person's gender, based on the existing choice field. This section
11
- explains how the field is defined, how we can customize its layout and finally,
12
- how we can register it for use in our application.
11
+ explains how the field is defined, how you can customize its layout and finally,
12
+ how you can register it for use in your application.
13
13
14
14
Defining the Field Type
15
15
-----------------------
16
16
17
- In order to create the custom field type, first we have to create the class
18
- representing the field. In our situation the class holding the field type
17
+ In order to create the custom field type, first you have to create the class
18
+ representing the field. In this situation the class holding the field type
19
19
will be called `GenderType ` and the file will be stored in the default location
20
20
for form fields, which is ``<BundleName>\Form\Type ``. Make sure the field extends
21
21
:class: `Symfony\\ Component\\ Form\\ AbstractType `::
@@ -54,8 +54,8 @@ for form fields, which is ``<BundleName>\Form\Type``. Make sure the field extend
54
54
The location of this file is not important - the ``Form\Type `` directory
55
55
is just a convention.
56
56
57
- Here, the return value of the ``getParent `` function indicates that we 're
58
- extending the ``choice `` field type. This means that, by default, we inherit
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
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:
@@ -86,7 +86,7 @@ The ``getName()`` method returns an identifier which should be unique in
86
86
your application. This is used in various places, such as when customizing
87
87
how your form type will be rendered.
88
88
89
- The goal of our field was to extend the choice type to enable selection of
89
+ The goal of this field was to extend the choice type to enable selection of
90
90
a gender. This is achieved by fixing the ``choices `` to a list of possible
91
91
genders.
92
92
@@ -97,11 +97,11 @@ Each field type is rendered by a template fragment, which is determined in
97
97
part by the value of your ``getName() `` method. For more information, see
98
98
:ref: `cookbook-form-customization-form-themes `.
99
99
100
- In this case, since our parent field is ``choice ``, we don't *need * to do
101
- any work as our custom field type will automatically be rendered like a ``choice ``
102
- type. But for the sake of this example, let's suppose that when our field
100
+ In this case, since tge parent field is ``choice ``, you don't *need * to do
101
+ any work as the custom field type will automatically be rendered like a ``choice ``
102
+ type. But for the sake of this example, let's suppose that when your field
103
103
is "expanded" (i.e. radio buttons or checkboxes, instead of a select field),
104
- we want to always render it in a ``ul `` element. In your form theme template
104
+ you want to always render it in a ``ul `` element. In your form theme template
105
105
(see above link for details), create a ``gender_widget `` block to handle this:
106
106
107
107
.. code-block :: html+jinja
@@ -172,12 +172,12 @@ Creating your Field Type as a Service
172
172
So far, this entry has assumed that you have a very simple custom field type.
173
173
But if you need access to configuration, a database connection, or some other
174
174
service, then you'll want to register your custom type as a service. For
175
- example, suppose that we 're storing the gender parameters in configuration:
175
+ example, suppose that you 're storing the gender parameters in configuration:
176
176
177
177
.. configuration-block ::
178
178
179
179
.. code-block :: yaml
180
-
180
+
181
181
# app/config/config.yml
182
182
parameters :
183
183
genders :
@@ -194,7 +194,7 @@ example, suppose that we're storing the gender parameters in configuration:
194
194
</parameter >
195
195
</parameters >
196
196
197
- To use the parameter, we'll define our custom field type as a service, injecting
197
+ To use the parameter, define your custom field type as a service, injecting
198
198
the ``genders `` parameter value as the first argument to its to-be-created
199
199
``__construct `` function:
200
200
@@ -225,8 +225,8 @@ the ``genders`` parameter value as the first argument to its to-be-created
225
225
for details.
226
226
227
227
Be sure that the ``alias `` attribute of the tag corresponds with the value
228
- returned by the ``getName `` method defined earlier. We 'll see the importance
229
- of this in a moment when we use the custom field type. But first, add a ``__construct ``
228
+ returned by the ``getName `` method defined earlier. You 'll see the importance
229
+ of this in a moment when you use the custom field type. But first, add a ``__construct ``
230
230
argument to ``GenderType ``, which receives the gender configuration::
231
231
232
232
// src/Acme/DemoBundle/Form/Type/GenderType.php
@@ -236,24 +236,24 @@ argument to ``GenderType``, which receives the gender configuration::
236
236
class GenderType extends AbstractType
237
237
{
238
238
private $genderChoices;
239
-
239
+
240
240
public function __construct(array $genderChoices)
241
241
{
242
242
$this->genderChoices = $genderChoices;
243
243
}
244
-
244
+
245
245
public function getDefaultOptions(array $options)
246
246
{
247
247
return array(
248
248
'choices' => $this->genderChoices,
249
249
);
250
250
}
251
-
251
+
252
252
// ...
253
253
}
254
254
255
255
Great! The ``GenderType `` is now fueled by the configuration parameters and
256
- registered as a service. And because we used the ``form.type `` alias in its
256
+ registered as a service. Additionally because you used the ``form.type `` alias in its
257
257
configuration, using the field is now much easier::
258
258
259
259
// src/Acme/DemoBundle/Form/Type/AuthorType.php
@@ -270,8 +270,8 @@ configuration, using the field is now much easier::
270
270
}
271
271
}
272
272
273
- Notice that instead of instantiating a new instance, we can just refer to
274
- it by the alias used in our service configuration, ``gender ``. Have fun!
273
+ Notice that instead of instantiating a new instance, you can just refer to
274
+ it by the alias used in your service configuration, ``gender ``. Have fun!
275
275
276
276
.. _`ChoiceType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
277
277
.. _`FieldType` : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
0 commit comments