Skip to content

Commit f6a4397

Browse files
committed
[Form] minor fixes in ChoiceType options
1 parent 3ba2ad8 commit f6a4397

File tree

7 files changed

+40
-36
lines changed

7 files changed

+40
-36
lines changed

reference/forms/types/choice.rst

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,31 @@ method::
9595
new Category('Cat3'),
9696
new Category('Cat4'),
9797
],
98-
'choice_label' => function(Category $category, $key, $value) {
99-
return strtoupper($category->getName());
98+
// a property path can by used to define the options below
99+
'choice_value' => 'name',
100+
// the empty value can be passed if there is a placeholder
101+
'choice_label' => function(?Category $category) {
102+
return $category ? strtoupper($category->getName()) : '';
100103
},
101-
'choice_attr' => function(Category $category, $key, $value) {
102-
return ['class' => 'category_'.strtolower($category->getName())];
104+
'choice_attr' => function(?Category $category) {
105+
return $category ? ['class' => 'category_'.strtolower($category->getName())] : [];
103106
},
104-
'group_by' => function(Category $category, $key, $value) {
107+
'group_by' => function() {
105108
// randomly assign things into 2 groups
106109
return rand(0, 1) == 1 ? 'Group A' : 'Group B';
107110
},
108-
'preferred_choices' => function(Category $category, $key, $value) {
109-
return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
111+
// the empty value maybe passed if a placeholder is used
112+
'preferred_choices' => function(?Category $category) {
113+
if (null === $category) {
114+
return false;
115+
}
116+
117+
return in_array($category->getName(), ['Cat2', 'Cat3'], true);
110118
},
111119
]);
112120

113-
You can also customize the `choice_name`_ and `choice_value`_ of each choice if
114-
you need further HTML customization.
121+
You can also customize the `choice_name`_ of each choice. You can learn more
122+
about all of them in the sections below.
115123

116124
.. _forms-reference-choice-tags:
117125

@@ -151,7 +159,7 @@ by passing a multi-dimensional ``choices`` array::
151159
.. image:: /_images/reference/form/choice-example4.png
152160
:align: center
153161

154-
To get fancier, use the `group_by`_ option.
162+
To get fancier, use the `group_by`_ option instead.
155163

156164
Field Options
157165
-------------
@@ -169,7 +177,10 @@ is the item's label and the array value is the item's value::
169177
// ...
170178

171179
$builder->add('inStock', ChoiceType::class, [
172-
'choices' => ['In Stock' => true, 'Out of Stock' => false],
180+
'choices' => [
181+
'In Stock' => true,
182+
'Out of Stock' => false,
183+
],
173184
]);
174185

175186
If there are choice values that are not scalar or the stringified

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
``choice_attr``
22
~~~~~~~~~~~~~~~
33

4-
**type**: ``array``, ``callable`` or ``string`` **default**: ``[]``
4+
**type**: ``array``, ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``[]``
55

66
Use this to add additional HTML attributes to each choice. This can be
77
an associative array where the keys match the choice keys and the values

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
``choice_label``
22
~~~~~~~~~~~~~~~~
33

4-
**type**: ``string``, ``callable`` or ``false`` **default**: ``null``
4+
**type**: ``string``, ``callable``, ``false`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``null``
55

6-
Normally, the array key of each item in the ``choices`` option is used as the
6+
By default, the array key of each item in the ``choices`` option is used as the
77
text that's shown to the user. The ``choice_label`` option allows you to take
88
more control::
99

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
``choice_name``
22
~~~~~~~~~~~~~~~
33

4-
**type**: ``callable`` or ``string`` **default**: ``null``
4+
**type**: ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``null``
55

66
Controls the internal field name of the choice. You normally don't care about this,
77
but in some advanced cases, you might. For example, this "name" becomes the index
8-
of the choice views in the template.
8+
of the choice views in the template and is used as part o the field name
9+
attribute.
910
1011
This can be a callable or a property path. See `choice_label`_ for similar usage.
11-
If ``null`` is used, an incrementing integer is used as the name.
12+
By default, the choice key or an incrementing integer may be used.
1213
1314
.. caution::
1415
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
``choice_value``
22
~~~~~~~~~~~~~~~~
33

4-
**type**: ``callable`` or ``string`` **default**: ``null``
4+
**type**: ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``null``
55

66
Returns the string "value" for each choice, which must be unique across all choices.
77
This is used in the ``value`` attribute in HTML and submitted in the POST/PUT requests.
88
You don't normally need to worry about this, but it might be handy when processing
99
an API request (since you can configure the value that will be sent in the API request).
1010
11-
This can be a callable or a property path. If ``null`` is given, an incrementing
12-
integer is used as the value.
11+
This can be a callable or a property path. By default, the choices are used if they
12+
can be casted to strings. Otherwise an incrementing integer is used.
1313
1414
If you pass a callable, it will receive one argument: the choice itself. When using
1515
the :doc:`/reference/forms/types/entity`, the argument will be the entity object
16-
for each choice or ``null`` in some cases, which you need to handle::
16+
for each choice or ``null`` in a placeholder is used, which you need to handle::
1717
18-
'choice_value' => function (MyOptionEntity $entity = null) {
18+
'choice_value' => function (?MyOptionEntity $entity) {
1919
return $entity ? $entity->getId() : '';
2020
},
21-
22-
.. caution::
23-
24-
In Symfony 2.7, there was a small backwards-compatibility break with how the
25-
``value`` attribute of options is generated. This is not a problem unless you
26-
rely on the option values in JavaScript. See `issue #14825`_ for details.
27-
28-
.. _`issue #14825`: https://github.com/symfony/symfony/pull/14825

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
``group_by``
22
~~~~~~~~~~~~
33

4-
**type**: ``string`` or ``callable`` **default**: ``null``
4+
**type**: ``string``, ``callable`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``null``
55

66
You can group the ``<option>`` elements of a ``<select>`` into ``<optgroup>``
77
by passing a multi-dimensional array to ``choices``. See the
@@ -25,9 +25,9 @@ Take the following example::
2525
'group_by' => function($choice, $key, $value) {
2626
if ($choice <= new \DateTime('+3 days')) {
2727
return 'Soon';
28-
} else {
29-
return 'Later';
3028
}
29+
30+
return 'Later';
3131
},
3232
]);
3333

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
``preferred_choices``
22
~~~~~~~~~~~~~~~~~~~~~
33

4-
**type**: ``array``, ``callable`` or ``string`` **default**: ``[]``
4+
**type**: ``array``, ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``[]``
55

66
This option allows you to move certain choices to the top of your list with a visual
77
separator between them and the rest of the options. If you have a form of languages,
8-
you can list the most popular on top, like Bork Bork and Pirate::
8+
you can list the most popular on top, like Bork and Pirate::
99

1010
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
1111
// ...
@@ -14,7 +14,7 @@ you can list the most popular on top, like Bork Bork and Pirate::
1414
'choices' => [
1515
'English' => 'en',
1616
'Spanish' => 'es',
17-
'Bork' => 'muppets',
17+
'Bork' => 'muppets',
1818
'Pirate' => 'arr',
1919
],
2020
'preferred_choices' => ['muppets', 'arr'],

0 commit comments

Comments
 (0)