【问题标题】:Symfony - Form : Listing Categories by typeSymfony - 表单:按类型列出类别
【发布时间】:2019-10-21 16:29:56
【问题描述】:

我有一个 formType,带有 EntityType 列

->add('categorie',        EntityType::class,
     [
                'class'                 => Category::class,
                'choice_label'          => 'title',
                'expanded'              => true,
                'multiple'              => true,
     ])

Category Entity 与 CategoryType Entity 有关系:

数据库上的类别类型表:

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上)通过在顶部添加类别类型的TITLE来修改显示列表?

【问题讨论】:

    标签: forms symfony


    【解决方案1】:

    是的,但您必须覆盖choice_widget_expanded 块。

    文章类型:

    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 %}
    

    更新:我最终得到了 Symfony ~4.3 的以下小部件:

    {%- block choice_widget_expanded -%}
        <div {{ block('widget_container_attributes') }}>
        {% for group_label, choice in choices %}
            <h4>{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}</h4>
            {% for choice in choice.choices %}
                {{- form_widget(form[choice.value]) -}}
                {{- form_label(form[choice.value], null, {translation_domain: choice_translation_domain}) -}}
            {% endfor %}
        {% endfor %}
        </div>
    {%- endblock choice_widget_expanded -%}
    

    edit.html.twig:

    {% extends 'base.html.twig' %}
    {% form_theme edit_form 'extended_form_layout.html.twig' %}
    
    {{ form(edit_form) }}
    

    【讨论】:

    • 感谢您的回答,您能解释一下在树枝上 for 循环上的 choices 是什么吗?
    • choices 实际上是您在 ArticleType 中使用 'group_by' 选项形成的组,但它们被称为选择项,因为此选项用于 ChoiceType,EntityType 表单域的父项。我没有让模板在 Symfony 4.3 下工作,但我找到了另一种方法。你使用哪个 Symfony 版本?
    • 我在最初的回答中为 Symfony 4.3 添加了一个小部件。
    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 2012-07-03
    • 2016-06-26
    • 2013-01-29
    • 2018-01-04
    • 2023-03-18
    • 2013-09-29
    • 1970-01-01
    相关资源
    最近更新 更多