簡體   English   中英

symfony2表單復選框分組(擴展和多選)

[英]symfony2 form checkbox grouped (expanded and multiple choice)

在我的表單類型我有這個:

    $builder
        ->add('options', 'choice', [
            'choices'  => $choices,
            'multiple' => true,
            'expanded' => true,
            'label'    => false
        ])
    ;

選擇是一個數組:

$choices = [
     'val1' => 'val1',
     'val2' => 'val2',
     'val3' => 'val3'
];

好極了! 現在我想用這樣的數組對我的選擇進行分類:

$choices = [
     'label1' => [
        'val1' => 'val1',
        'val2' => 'val2',
     ],
     'label2' => [
        'val3' => 'val3',
        'val4' => 'val4',
     ],
     'label3' => [
        'val5' => 'val5',
        'val6' => 'val6',
     ],
];

所以我想要下面的結果

在此輸入圖像描述

實現這一目標的最佳方法是什么?

您可以覆蓋此選擇字段的窗口小部件並手動呈現標簽

像這樣(在你的形式tamplate):

{% form_theme putYourFormNameHere _self %}

{% block _putYourFormNameHere_options_widget %}
    <div {{ block('widget_container_attributes') }}>
    {% for group_label, group in choices %}
        {%- if group is iterable -%}
        <div>
            <label><b>{{ group_label|trans({}, translation_domain) }}</b></label>
            {% for key, choice in group %}
                <div>
                    {{- form_widget(form[key]) -}}
                    {{- form_label(form[key]) -}}
                <div>
            {% endfor %}
        </div>
        {%- endif -%}
    {% endfor %}
{% endblock %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM