@@ -82,8 +82,8 @@ This will create a ``select`` drop-down like this:
8282
8383If the user selects ``No ``, the form will return ``false `` for this field. Similarly,
8484if the starting data for this field is ``true ``, then ``Yes `` will be auto-selected.
85- In other words, the **value ** of each item is the value you want to get/set in PHP
86- code, while the **key ** is what will be shown to the user.
85+ In other words, the **choice ** of each item is the value you want to get/set in PHP
86+ code, while the **key ** is the ** label ** that will be shown to the user.
8787
8888Advanced Example (with Objects!)
8989--------------------------------
@@ -103,23 +103,40 @@ method::
103103 new Category('Cat3'),
104104 new Category('Cat4'),
105105 ],
106- 'choice_label' => function(Category $category, $key, $value) {
107- return strtoupper($category->getName());
106+ // "name" is a property path, meaning Symfony will look for a public
107+ // property or a public method like "getName()" to define the input
108+ // string value that will be submitted by the form
109+ 'choice_value' => 'name',
110+ // a callback to return the label for a given choice
111+ // if a placeholder is used, its empty value (null) may be passed but
112+ // its label is defined by its own "placeholder" option
113+ 'choice_label' => function(?Category $category) {
114+ return $category ? strtoupper($category->getName()) : '';
108115 },
109- 'choice_attr' => function(Category $category, $key, $value) {
110- return ['class' => 'category_'.strtolower($category->getName())];
116+ // returns the html attributes for each option input (may be radio/checkbox)
117+ 'choice_attr' => function(?Category $category) {
118+ return $category ? ['class' => 'category_'.strtolower($category->getName())] : [];
111119 },
112- 'group_by' => function(Category $category, $key, $value) {
120+ // every option can use a string property path or any callable that get
121+ // passed each choice as argument, but it may not be needed
122+ 'group_by' => function() {
113123 // randomly assign things into 2 groups
114124 return rand(0, 1) == 1 ? 'Group A' : 'Group B';
115125 },
116- 'preferred_choices' => function(Category $category, $key, $value) {
117- return $category->getName() == 'Cat2' || $category->getName() == 'Cat3';
126+ // a callback to return whether a category is preferred
127+ 'preferred_choices' => function(?Category $category) {
128+ return $category && 100 < $category->getArticleCounts();
118129 },
119130 ]);
120131
121- You can also customize the `choice_name `_ and `choice_value `_ of each choice if
122- you need further HTML customization.
132+ You can also customize the `choice_name `_ of each choice. You can learn more
133+ about all of these options in the sections below.
134+
135+ .. caution ::
136+
137+ The *placeholder * is a specific field, when the choices are optional the
138+ first item in the list must be empty, so the user can unselect.
139+ Be sure to always handle the empty choice ``null `` when using callbacks.
123140
124141.. _forms-reference-choice-tags :
125142
@@ -159,7 +176,7 @@ by passing a multi-dimensional ``choices`` array::
159176.. image :: /_images/reference/form/choice-example4.png
160177 :align: center
161178
162- To get fancier, use the `group_by `_ option.
179+ To get fancier, use the `group_by `_ option instead .
163180
164181Field Options
165182-------------
@@ -177,7 +194,10 @@ is the item's label and the array value is the item's value::
177194 // ...
178195
179196 $builder->add('inStock', ChoiceType::class, [
180- 'choices' => ['In Stock' => true, 'Out of Stock' => false],
197+ 'choices' => [
198+ 'In Stock' => true,
199+ 'Out of Stock' => false,
200+ ],
181201 ]);
182202
183203If there are choice values that are not scalar or the stringified
0 commit comments