1
votes

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?

1

1 Answers

5
votes

I did the same modifying the following block

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

You will still need to remove the original label, you can modify it with javascript, hide it with css or modify the template so it renders conditionally, whatever is best for you.

This question might also help: Symfony2 - How to put label and input for checkboxes/radios in a same line?