Skip to content

Commit 6365c01

Browse files
committed
[#6297] Further clarifying this option
1 parent 3a478a2 commit 6365c01

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ choice_value
66

77
**type**: ``callable`` or ``string`` **default**: ``null``
88

9-
Returns the string "value" for each choice. This is used in the ``value`` attribute
10-
in HTML and submitted in the POST/PUT requests. You don't normally need to worry
11-
about this, but it might be handy when processing an API request (since you can
12-
configure the value that will be sent in the API request).
9+
Returns the string "value" for each choice, which must be unique across all choices.
10+
This is used in the ``value`` attribute in HTML and submitted in the POST/PUT requests.
11+
You don't normally need to worry about this, but it might be handy when processing
12+
an API request (since you can configure the value that will be sent in the API request).
1313

1414
This can be a callable or a property path. If ``null`` is given, an incrementing
1515
integer is used as the value.
1616

17-
.. note::
17+
If you pass a callable, it will receive one argument: the choice itself. When using
18+
the :doc:`/references/forms/types/entity`, the argument will be the entity object
19+
for each choice or ``null`` in some cases, which you need to handle:
1820

19-
In contrast to other choice options (e.g. `choice_label`_), the callable for
20-
the ``choice_value`` option takes as **only** argument the actual choice, which
21-
is also the value that gets passed to your model when submitting the form.
21+
.. code-block:: php
2222

23-
If you are using a callable to populate ``choice_value``, you need to check
24-
for the case that the value of the field may be ``null``.
23+
'choice_value' => function (MyOptionEntity $entity = null) {
24+
return $entity ? $entity->getId() : '';
25+
},
2526

2627
.. caution::
2728

0 commit comments

Comments
 (0)