Skip to content

Commit 7392fc2

Browse files
committed
minor #17384 [Form][Form Choice] customize choice entry (spike31)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Form][Form Choice] customize choice entry First PR here #15921, but...something happened! I'm not sure where to add the link on the form_theme document, so I don't add it. And I'm not sure for the title **Access data in a Form Choice** `@HeahDude` Commits ------- 8a05949 [Form][Form Choice] customize choice entry
2 parents de881b3 + 8a05949 commit 7392fc2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

reference/forms/types/choice.rst

Lines changed: 39 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy