首页 > 解决方案 > 如何在 Symfony 4 表单上的多个复选框周围添加包装器

问题描述

我有一个在表单中创建的复选框列表,其中包含以下内容

->add('ISPreNbStudents', ChoiceType::class, [
            'multiple' => false,
            'choices'  => [
                '1' => 1,
                '2' => 2,
                '3' => 3,
                '4' => 4,
                '5' => 5,
            ],
            'expanded' => true
        ])

然后我可以在树枝文件中显示这个:{{ form_widget(form.ISOptMonths) }}

问题是它现在显示标签列表和输入,如下所示

<input type="radio" id="availability_ISPreNbStudents_0" name="availability[ISPreNbStudents]" value="1">
<label for="availability_ISPreNbStudents_0">1</label> 
<input type="radio" id="availability_ISPreNbStudents_1" name="availability[ISPreNbStudents]" value="2">
<label for="availability_ISPreNbStudents_1">2</label>

我需要像这样在每个标签/输入周围放置包装器

<div class="styled-input-single">
    <input type="checkbox" name="case-1" id="1" />
    <label for="1">1</label>
</div>
<div class="styled-input-single">
    <input type="checkbox" name="case-2" id="2" />
    <label for="2">2</label>
</div>

我怎样才能做到这一点?

标签: formssymfonytwig

解决方案


我终于找到了解决方案:

这是您打印列表的方式:

{{ form_row(registrationForm.firstRecourse) }}

现在您是否要在每个选项周围添加包装器:

<div class="row">
    {{ form_label(registrationForm.ISPreNbStudents) }}
    {% for checkbox in registrationForm.ISPreNbStudents.children %}
        <div class="styled-input-single">
            {{ form_widget(checkbox) }}
            {{ form_label(checkbox) }}
            {{ form_errors(checkbox) }}
        </div>
    {% endfor %}
</div>

推荐阅读