Skip to content

Commit b44c2b3

Browse files
committed
Incorporate feedback on symfony#6393
1 parent 95950f5 commit b44c2b3

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

reference/forms/types/choice.rst

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ This will create a ``select`` drop-down like this:
7171
.. image:: /_images/reference/form/choice-example1.png
7272
:align: center
7373

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``.
8280

8381
.. caution::
8482

@@ -90,11 +88,12 @@ form and used in the template for the corresponding html attribute.
9088

9189
.. note::
9290

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::
9897

9998
$builder->add('isAttending', 'choice', array(
10099
'choices' => array(
@@ -148,18 +147,18 @@ you need further HTML customization.
148147

149148
.. caution::
150149

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``.
153152
In the example above, the default values are incrementing integers if the
154153
``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::
156155

157156
$builder->add('category', 'choice', array(
158157
'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'),
163162
),
164163
'choices_as_values' => true,
165164
'choice_value' => function(Category $category = null) {
@@ -173,8 +172,10 @@ you need further HTML customization.
173172
return strtoupper($category->getName());
174173
},
175174
'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.
178179
));
179180

180181
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::
238239
'choices_as_values' => true,
239240
));
240241

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.
244247

245248
.. include:: /reference/forms/types/options/choice_attr.rst.inc
246249

@@ -299,14 +302,14 @@ choice_loader
299302

300303
**type**: :class:`Symfony\\Component\\Form\\ChoiceList\\Loader\\ChoiceLoaderInterface`
301304

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
305308
submitted choices will be loaded.
306309

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).
310313

311314
.. include:: /reference/forms/types/options/choice_name.rst.inc
312315

@@ -326,7 +329,7 @@ choices_as_values
326329
The ``choices_as_values`` option was added to keep backward compatibility with the
327330
*old* way of handling the ``choices`` option. When set to ``false`` (or omitted),
328331
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.
330333

331334
* Before 2.7 (and deprecated now)::
332335

reference/forms/types/options/choice_label.rst.inc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ can be a string :ref:`property path <reference-form-option-property-path>`. Imag
2424
'choice_label' => 'displayName',
2525
));
2626

27-
If set as a callable is called for each choice, passing you its model data ``$choice``
28-
and the ``$key`` from the choices array (default label). ``$value`` is either
29-
`choice_value`_ option value when defined or the default string value of the choice::
27+
If set as a callable, your function is called for each choice, passing the
28+
model data ``$choice`` and the ``$key`` from the choices array (the default
29+
label)::
3030

3131
$builder->add('attending', 'choice', array(
3232
'choices' => array(
@@ -39,6 +39,7 @@ and the ``$key`` from the choices array (default label). ``$value`` is either
3939
if (true === $choice) {
4040
return 'Definitely!';
4141
}
42+
4243
return strtoupper($key);
4344

4445
// or if you want to translate some key

0 commit comments

Comments
 (0)