Skip to content

Commit 8d14684

Browse files
committed
Merge branch '6.1' into 6.2
* 6.1: [Form][Form Choice] customize choice entry
2 parents e792002 + 98e3692 commit 8d14684

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
@@ -322,5 +322,41 @@ Field Variables
322322

323323
.. tip::
324324

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

0 commit comments

Comments
 (0)