@@ -71,14 +71,12 @@ This will create a ``select`` drop-down like this:
71
71
.. image :: /_images/reference/form/choice-example1.png
72
72
:align: center
73
73
74
- The model data of this field, the **choice ** may be any of the ``choices `` option
75
- values, while **keys ** are used as default label that the user will see and select.
76
-
77
- If the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
78
- In other words, each value of the ``choices `` option is the **choice ** data you
79
- want to deal with in PHP code, while the **key ** is the default label that will be
80
- shown to the user and the **value ** is the string that will be submitted to the
81
- form and used in the template for the corresponding html attribute.
74
+ Each choice defined in the ``choices `` option consists of a **key ** containing
75
+ the label (e.g. ``Yes ``) that will be shown to the user and a **value **
76
+ containing the PHP data (e.g. ``true ``) you want to retrieve from the field.
77
+ This means that if you manually set the field data to ``true ``, the user will
78
+ see ``Yes `` as the selected choice. If the user selects ``No ``, the returned
79
+ data will be ``false ``.
82
80
83
81
.. caution ::
84
82
@@ -90,11 +88,12 @@ form and used in the template for the corresponding html attribute.
90
88
91
89
.. note ::
92
90
93
- Pre selected choices will depend on the **data ** passed to the field and
94
- the values of the ``choices `` option. However submitted choices will depend
95
- on the **string ** matching the **choice **. In the example above, the default
96
- values are incrementing integers because ``null `` cannot be casted to string.
97
- You should consider it as well when dealing with ``empty_data `` option::
91
+ The **value ** (e.g. ``true ``) of a choice is converted to a string and used
92
+ in the ``value `` attribute in HTML and submitted in the POST/PUT requests.
93
+ In cases where one of the values can't be converted to a string
94
+ (e.g. ``null `` like in the example above), the values will be rendered
95
+ as incrementing integers. You should consider it as well when dealing with
96
+ the ``empty_data `` option::
98
97
99
98
$builder->add('isAttending', 'choice', array(
100
99
'choices' => array(
@@ -148,18 +147,18 @@ you need further HTML customization.
148
147
149
148
.. caution ::
150
149
151
- When dealing with objects as choices, you should be careful about how
152
- string values are set to use them with the `empty_data ` option .
150
+ When dealing with objects as choices, if you need to set the
151
+ `` empty_data `` option, you may need to override the `` choice_value `` .
153
152
In the example above, the default values are incrementing integers if the
154
153
``Category `` class does not implement ``toString `` method.
155
- To get a full control of the string values use the `choice_value `_ option::
154
+ To get full control of the string values use the `choice_value `_ option::
156
155
157
156
$builder->add('category', 'choice', array(
158
157
'choices' => array(
159
- new Category('Cat1'),
160
- new Category('Cat2'),
161
- new Category('Cat3'),
162
- new Category('Cat4'),
158
+ new Category('Cat1'),
159
+ new Category('Cat2'),
160
+ new Category('Cat3'),
161
+ new Category('Cat4'),
163
162
),
164
163
'choices_as_values' => true,
165
164
'choice_value' => function(Category $category = null) {
@@ -173,8 +172,10 @@ you need further HTML customization.
173
172
return strtoupper($category->getName());
174
173
},
175
174
'multiple' => true,
176
- 'empty_data' => array('cat2'), // default submitted value
177
- // an array because of multiple option
175
+ 'empty_data' => array('cat2'), // the default submitted value, matches
176
+ // a value of the choice_value option.
177
+ // passed as an array because the multiple
178
+ // option is true.
178
179
));
179
180
180
181
Note that `choice_value `_ option set as a callable can get passed ``null ``
@@ -238,9 +239,11 @@ is the choice's label and the array value is the choice's data::
238
239
'choices_as_values' => true,
239
240
));
240
241
241
- The component will try to cast the choices data to string to use it in view
242
- format, in that case ``"0" `` and ``"1" ``, but you can customize it using the
243
- `choice_value `_ option.
242
+ The field will try to cast the choice values (e.g. ``true `` and ``false ``) into
243
+ strings to be rendered in HTML (in this case, ``"0" `` and ``"1"` ``). In the case
244
+ that one of the values can't be casted to a string, the values will be rendered
245
+ as incrementing integers. You can also customize these strings by using
246
+ the `choice_value `_ option.
244
247
245
248
.. include :: /reference/forms/types/options/choice_attr.rst.inc
246
249
@@ -299,14 +302,14 @@ choice_loader
299
302
300
303
**type **: :class: `Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface `
301
304
302
- The ``choice_loader `` can be used to load the choices form a data source with a
303
- custom logic (e.g query language) such as database or search engine.
304
- The list will be fully loaded to display the form, but while submission only the
305
+ The ``choice_loader `` can be used to load the choices from a data source with
306
+ custom logic (e.g. query language) such as a database or a search engine.
307
+ The list will be fully loaded to display the form, but on submission, only the
305
308
submitted choices will be loaded.
306
309
307
- Also, the :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Factory\\ ChoiceListFactoryInterface` ` will cache the choice list
308
- so the same :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface` ` can be used in different fields with more performance
309
- (reducing N queries to 1).
310
+ Also, the :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Factory\\ ChoiceListFactoryInterface` `
311
+ will cache the choice list so the same :class: ``Symfony\\ Component\\ Form\\ ChoiceList\\ Loader\\ ChoiceLoaderInterface` `
312
+ can be used in different fields with more performance (reducing N queries to 1).
310
313
311
314
.. include :: /reference/forms/types/options/choice_name.rst.inc
312
315
@@ -326,7 +329,7 @@ choices_as_values
326
329
The ``choices_as_values `` option was added to keep backward compatibility with the
327
330
*old * way of handling the ``choices `` option. When set to ``false `` (or omitted),
328
331
the choice keys are used as the view value and the choice values are shown
329
- to the user as label.
332
+ to the user as the label.
330
333
331
334
* Before 2.7 (and deprecated now)::
332
335
0 commit comments