Skip to content

Commit c8b073f

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: [Form][Form Choice] customize choice entry
2 parents f6f0755 + bb06b33 commit c8b073f

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
@@ -314,5 +314,41 @@ Field Variables
314314

315315
.. tip::
316316

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

0 commit comments

Comments
 (0)