Forms Symfony-表单:按类型列出类别

Forms Symfony-表单:按类型列出类别,forms,symfony,Forms,Symfony,我有一个formType,带有EntityType列 ->add('categorie', EntityType::class, [ 'class' => Category::class, 'choice_label' => 'title', 'expanded' => true

我有一个formType,带有EntityType列

->add('categorie',        EntityType::class,
     [
                'class'                 => Category::class,
                'choice_label'          => 'title',
                'expanded'              => true,
                'multiple'              => true,
     ])
类别实体与CategoryType实体有关系:

数据库上的CategoryType表:

ID | TITLE
1  | Sport
2  | policy
类别表:

ID | TITILE | ID_CATEGORY_TYPE
1  | Soccer | 1
2  |  Rugby | 1
3  |   USA  | 2
4  | Russia | 2
我想在表单createView()上按类别类型列出类别,如下所示

Sport
------
[] Soccer //Type Checkbox
[] Rugby  

Policy
------
[] USA
[] RUSSIA

是否有任何方法(在FormType上)通过在顶部添加类别类型的标题来修改显示列表?

是,但您必须覆盖选项\u widget\u扩展块

物品类型:

use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Category;

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('category', null, [
                    'expanded' => true,
                    'multiple' => true,
                    // make groups with the CategoryType entity
                    'group_by' => function(Category $category, $key, $value) {
                        return $category->getType()->getName();
                    },
                    // sort the categoryTypes alpabetical
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                                ->join('c.type', 't')
                                ->orderBy('t.name', 'ASC');
                    },
                ])
        ;
    }
}
extended_form_layout.html.twig:

{% block choice_widget_expanded -%}
    {% if '-inline' in label_attr.class|default('') -%}
        <div class="control-group">
            {%- for child in form %}
                {{- form_widget(child, {
                    parent_label_class: label_attr.class|default(''),
                    translation_domain: choice_translation_domain,
                }) -}}
            {% endfor -%}
        </div>
    {%- else -%}
    <div {{ block('widget_container_attributes') }}>
        <div class="row">
        {% for group_label, choice in choices %}
            <div class="col-lg-3 col-md-4 col-sm-4 col-xs-6">
            <h4>{{ group_label }}</h4>
            {% for choice in choice.choices %}
                {{- form_widget(form[choice.value], {
                        parent_label_class: label_attr.class|default(''),
                        translation_domain: choice_translation_domain,
                    }) -}}
            {% endfor -%}
            </div>
            {% if not(loop.index % 3) %}</div><div class="row">{% endif %}
        {% endfor -%}
        </div>
    </div>
    {%- endif %}
{%- endblock choice_widget_expanded %}
{% extends 'base.html.twig' %}
{% form_theme edit_form 'extended_form_layout.html.twig' %}

{{ form(edit_form) }}

感谢您的回答,您能解释一下什么是树枝上循环的
choices
吗?选项实际上是您在ArticleType中使用“group\u by”选项形成的组,但它们被称为选项,因为此选项是对ChoiceType(EntityType表单字段的父级)的补充。我没有让模板在Symfony 4.3下工作,但我找到了另一种方法。您使用哪个Symfony版本?我在最初的回答中添加了Symfony 4.3的小部件。
{% extends 'base.html.twig' %}
{% form_theme edit_form 'extended_form_layout.html.twig' %}

{{ form(edit_form) }}