@@ -323,3 +323,42 @@ Field Variables
323
323
324
324
It's significantly faster to use the :ref: `selectedchoice <form-twig-selectedchoice >`
325
325
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