@@ -74,8 +74,8 @@ This will create a ``select`` drop-down like this:
7474
7575If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
7676if 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
8080Advanced 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
156173Field 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
175195If there are choice values that are not scalar or the stringified
0 commit comments