<p dir="auto"><code class="notranslate">choices</code> can be a nested array, there is also a <code class="notranslate">group_by</code> option in <code class="notranslate">entity</code> type, those can be used to group entities in HTML <code class="notranslate">optgroup</code> tags, but this only work for <code class="notranslate">select</code> widgets.</p>
<p dir="auto">Shouldn't this also be applied to <code class="notranslate">checkboxes</code> and <code class="notranslate">radios</code> (with a fieldset and a legend)?</p>
<p dir="auto">Might help for the use case described in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="6286728" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/5286" data-hovercard-type="issue" data-hovercard-url="/symfony/symfony/issues/5286/hovercard" href="https://github.com/symfony/symfony/issues/5286">#5286</a>.</p>
<p dir="auto">When using <code class="notranslate">"expanded" =&gt; true</code> on a <code class="notranslate">choice</code> field that contains groups, the default form theme simply renders all radio buttons/checkboxes sequentially, without headings or any other indication of the group structure (at least under Twig).</p>
<p dir="auto">From what I can tell, this issue stems from the fact that the choices in an expanded choice field are written directly from the <code class="notranslate">FormView</code>'s <code class="notranslate">choices</code> variable, which is structured into groups. By contrast, expanded choice fields are represented using a series of checkbox/radio button fields, which are treated as children of the choice field - the problem being that the children are not arranged in any hierarchical structure.</p>
<p dir="auto">The <code class="notranslate">FormView</code> for an expanded choice field does still have a <code class="notranslate">choices</code> variable like that of a collapsed choice field, but currently there is no effective way to match up the <code class="notranslate">ChoiceView</code> object with the corresponding child <code class="notranslate">FormView</code> in the template, so rendering the fields from the <code class="notranslate">choices</code> variable as with a collapsed choice field is not a viable option.</p>