Skip to content

Commit 98e3692

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [Form][Form Choice] customize choice entry
2 parents 6563914 + a2fadf0 commit 98e3692

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

reference/forms/types/choice.rst

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,41 @@ Field Variables
309309

310310
.. tip::
311311

312-
It's significantly faster to use the :ref:`selectedchoice <form-twig-selectedchoice>`
313-
test instead when using Twig.
312+
In Twig template, instead of using ``is_selected()``, it's significantly
313+
faster to use the :ref:`selectedchoice <form-twig-selectedchoice>` test.
314+
315+
Accessing Form Choice Data
316+
...........................
317+
318+
The ``form.vars`` variable of each choice entry holds data such as whether the
319+
choice is selected or not. If you need to get the full list of choices data and
320+
values, use the ``choices`` variable from the parent form of the choice entry
321+
(which is the ``ChoiceType`` itself) with ``form.parent.vars.choices``::
322+
323+
.. code-block:: html+twig
324+
325+
{# `true` or `false`, whether the current choice is selected as radio or checkbox #}
326+
{{ form.vars.data }}
327+
328+
{# the current choice value (i.e a category name when `'choice_value' => 'name'` #}
329+
{{ form.vars.value }}
330+
331+
{# a map of `ChoiceView` or `ChoiceGroupView` instances indexed by choice values or group names #}
332+
{{ form.parent.vars.choices }}
333+
334+
Following the same advanced example as above (where choices values are entities),
335+
the ``Category`` object is inside ``form.parent.vars.choices[key].data``::
336+
337+
.. code-block:: html+twig
338+
339+
{% block _form_categories_entry_widget %}
340+
{% set entity = form.parent.vars.choices[form.vars.value].data %}
341+
342+
<tr>
343+
<td>{{ form_widget(form) }}</td>
344+
<td>{{ form.vars.label }}</td>
345+
<td>
346+
{{ entity.name }} | {{ entity.group }}
347+
</td>
348+
</tr>
349+
{% endblock %}

0 commit comments

Comments
 (0)