Skip to content

Commit 8a05949

Browse files
gilles-gjaviereguiluz
authored andcommitted
[Form][Form Choice] customize choice entry
1 parent 8e18cc7 commit 8a05949

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

reference/forms/types/choice.rst

+39
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,42 @@ Field Variables
323323

324324
It's significantly faster to use the :ref:`selectedchoice <form-twig-selectedchoice>`
325325
test instead when using Twig.
326+
327+
328+
Access data in a Form Choice
329+
.............................
330+
331+
When you use an expanded ``ChoiceType`` and need to customize the children ``entry`` blocks,
332+
the ``form.vars`` of entries (radio button or checkbox) may not be enough since each holds a
333+
boolean value meaning whether a choice is selected or not.
334+
To get the full list of choices data and values, you will need to access the ``choices`` variable
335+
from their parent form (the ``ChoiceType`` itself) with ``form.parent.vars.choices``::
336+
337+
Given the advanced object example, each entry would have access to the following variables:
338+
339+
.. code-block:: html+twig
340+
341+
{# `true` or `false`, whether the current choice is selected as radio or checkbox #}
342+
{{ form.vars.data }}
343+
344+
{# the current choice value (i.e a category name when `'choice_value' => 'name'` #}
345+
{{ form.vars.value }}
346+
347+
{# a map of `ChoiceView` or `ChoiceGroupView` instances indexed by choice values or group names #}
348+
{{ form.parent.vars.choices }}
349+
350+
So the Category's entity is inside ``form.parent.vars.choices[key].data``, because the parent knows all the choices.
351+
352+
.. code-block:: html+twig
353+
354+
{% block _form_categories_entry_widget %}
355+
{% set entity = form.parent.vars.choices[form.vars.value].data %}
356+
357+
<tr>
358+
<td>{{ form_widget(form) }}</td>
359+
<td>{{ form.vars.label }}</td>
360+
<td>
361+
{{ entity.name }} | {{ entity.group }}
362+
</td>
363+
</tr>
364+
{% endblock %}

0 commit comments

Comments
 (0)