@@ -6,22 +6,23 @@ choice_value
6
6
7
7
**type**: ``callable`` or ``string`` **default**: ``null``
8
8
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).
13
13
14
14
This can be a callable or a property path. If ``null`` is given, an incrementing
15
15
integer is used as the value.
16
16
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:
18
20
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
22
22
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
+ },
25
26
26
27
.. caution::
27
28
0 commit comments