1

I try to create a custom form theme for my project where I want to render all checkbox fields INSIDE the label like:

<label><input type="checkbox" /><label>

I've found out that I have to change the choice_widget_expanded block for this:

{% block choice_widget_expanded %}
    {% spaceless %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
            {{ form_label(child) }}
        {% endfor %}
        </div>
    {% endspaceless %}
{% endblock choice_widget_expanded %}

The problem is, when I copy the content of the form_label block into the container instead of calling form_label(child), I don't really see how the block accesses the variable passed(which was child when I called the function), and how to call the form_widget function in the form_label block:

{% block form_label %}
    <label>{{ form_widget(?? what to put here??) }}</label>
{% endblock form_label %}

Also, if I create a block with a different name, like "form_label_extra" and try to call it, it throws an error, because it's not a registered twig function.

Does anyone know how this variables are passed between the form blocks, and how to achieve my goal?

4

1 に答える 1

5

次のブロックを変更して同じことを行いました

{% block checkbox_widget %}
{% spaceless %}
<label class="button i_clear"> 
    <input type="checkbox"
        {{ block('widget_attributes') }}
        {% if value is defined %} value="{{ value }}"{% endif %}
        {% if checked %} checked="checked"{% endif %} />
    <span>{{ label }}</span>
</label>
{% endspaceless %}
{% endblock checkbox_widget %}

元のラベルを削除する必要があります。javascript で変更するか、css で非表示にするか、条件付きでレンダリングされるようにテンプレートを変更することができます。

この質問も役立つかもしれません: Symfony2 - チェックボックス/ラジオのラベルと入力を同じ行に配置する方法は?

于 2013-01-18T19:50:01.590 に答える