Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4ee4102

Browse filesBrowse files
HeahDudeOskarStark
authored andcommitted
[Form] minor fixes in ChoiceType options
1 parent 3ba2ad8 commit 4ee4102
Copy full SHA for 4ee4102

7 files changed

+51
-38
lines changed

‎reference/forms/types/choice.rst

Copy file name to clipboardExpand all lines: reference/forms/types/choice.rst
+33-13Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ This will create a ``select`` drop-down like this:
7474

7575
If the user selects ``No``, the form will return ``false`` for this field. Similarly,
7676
if the starting data for this field is ``true``, then ``Yes`` will be auto-selected.
77-
In other words, the **value** of each item is the value you want to get/set in PHP
78-
code, while the **key** is what will be shown to the user.
77+
In other words, the **choice** of each item is the value you want to get/set in PHP
78+
code, while the **key** is the **label** that will be shown to the user.
7979

8080
Advanced Example (with Objects!)
8181
--------------------------------
@@ -95,23 +95,40 @@ method::
9595
new Category('Cat3'),
9696
new Category('Cat4'),
9797
],
98-
'choice_label' => function(Category $category, $key, $value) {
99-
return strtoupper($category->getName());
98+
// "name" is a property path, meaning Symfony will look for a public
99+
// property or a public method like "getName()" to define the input
100+
// string value that will be submitted by the form
101+
'choice_value' => 'name',
102+
// a callback to return the label for a given choice
103+
// if a placeholder is used, its empty value (null) may be passed but
104+
// its label is defined by its own "placeholder" option
105+
'choice_label' => function(?Category $category) {
106+
return $category ? strtoupper($category->getName()) : '';
100107
},
101-
'choice_attr' => function(Category $category, $key, $value) {
102-
return ['class' => 'category_'.strtolower($category->getName())];
108+
// returns the html attributes for each option input (may be radio/checkbox)
109+
'choice_attr' => function(?Category $category) {
110+
return $category ? ['class' => 'category_'.strtolower($category->getName())] : [];
103111
},
104-
'group_by' => function(Category $category, $key, $value) {
112+
// every option can use a string property path or any callable that get
113+
// passed each choice as argument, but it may not be needed
114+
'group_by' => function() {
105115
// randomly assign things into 2 groups
106116
return rand(0, 1) == 1 ? 'Group A' : 'Group B';
107117
},
108-
'preferred_choices' => function(Category $category, $key, $value) {
109-
return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
118+
// a callback to return whether a category is preferred
119+
'preferred_choices' => function(?Category $category) {
120+
return $category && 100 < $category->getArticleCounts();
110121
},
111122
]);
112123

113-
You can also customize the `choice_name`_ and `choice_value`_ of each choice if
114-
you need further HTML customization.
124+
You can also customize the `choice_name`_ of each choice. You can learn more
125+
about all of these options in the sections below.
126+
127+
.. caution::
128+
129+
The *placeholder* is a specific field, when the choices are optional the
130+
first item in the list must be empty, so the user can unselect.
131+
Be sure to always handle the empty choice ``null`` when using callbacks.
115132

116133
.. _forms-reference-choice-tags:
117134

@@ -151,7 +168,7 @@ by passing a multi-dimensional ``choices`` array::
151168
.. image:: /_images/reference/form/choice-example4.png
152169
:align: center
153170

154-
To get fancier, use the `group_by`_ option.
171+
To get fancier, use the `group_by`_ option instead.
155172

156173
Field Options
157174
-------------
@@ -169,7 +186,10 @@ is the item's label and the array value is the item's value::
169186
// ...
170187

171188
$builder->add('inStock', ChoiceType::class, [
172-
'choices' => ['In Stock' => true, 'Out of Stock' => false],
189+
'choices' => [
190+
'In Stock' => true,
191+
'Out of Stock' => false,
192+
],
173193
]);
174194

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

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

Copy file name to clipboardExpand all lines: reference/forms/types/options/choice_attr.rst.inc
+1-1Lines 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

Copy file name to clipboardExpand all lines: reference/forms/types/options/choice_label.rst.inc
+2-2Lines 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

Copy file name to clipboardExpand all lines: reference/forms/types/options/choice_name.rst.inc
+4-3Lines 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 (starting at ``0``).
1213
1314
.. caution::
1415
+5-13Lines 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 (starting at ``0``).
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

Copy file name to clipboardExpand all lines: reference/forms/types/options/group_by.rst.inc
+3-3Lines 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

Copy file name to clipboardExpand all lines: reference/forms/types/options/preferred_choices.rst.inc
+3-3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.