Symfony2 - form_widget - 复选框分开

时间:2015-09-24 17:23:16

标签: symfony twig

我有这个:

<fieldset>
    <legend>{{ form_label(form.fees, 'fees' | trans , { 'label_attr': {'class': 'col-sm-3 control-label'} }) }}</legend>
    <div class="large-12 columns fees">
        <br/>
        {{ form_errors(form.fees) }}
        <div class="col-sm-5">
            {{ form_widget(form.fees, { 'attr': {'class': 'form-control validate[required]'} }) }}
        </div>
    </div>
</fieldset>

这一行:
{{ form_widget(form.fees, { 'attr': {'class': 'form-control validate[required]'} }) }}

打印:

<label></label>
<input />
<label></label>
<input />

但我需要这样的东西(包装纸):

<div><label></label><input /></div>
<div><label></label><input /></div>

TNX !! :)

2 个答案:

答案 0 :(得分:3)

如果您希望拥有四个复选框的行,则类似于:

{# Wrap span around checkboxes #}
{{ form_label(form.fees) }}
{{ form_errors(form.fees) }}<br>
{% for batch in form.fees|batch(4) %}
    <div class="batchRow">
        {% for option in batch %}
            <div class="yourClassName">
                {{ form_label(option) }}
                {{ form_widget(option) }}
            </div>
        {% endfor %}
    </div>
{% endfor %}

否则你可以摆脱批量水平。

答案 1 :(得分:0)

您可以使用自定义单个字段。例如,您有ProductType表单,而您的案例是收费的。因此,您可以在顶部代码中添加此行:

{# tell symfony use form theme #}
{% form_theme form _self %}

{# the block customize #}
{% block _product_fees_widget %}
    <div>
        {{ block('form_widget_simple') }}
    </div>
{% endblock %}

{# print #}
{{ form_widget(form.fees, { 'attr': {'class': 'form-control validate[required]'} }) }}

有关详细信息,请阅读 FORM THEME