Symfony2 how to render Checkboxes?

user747252 picture user747252 · Dec 19, 2011 · Viewed 18.9k times · Source

I have a formbuilder form with a multiple choice list of countries. When I render them on my form like this:

{{ form_widget(edit_form.countries) }}

They appear like this:

<input type="checkbox" name="..." value="AD" /><label for="...">Andorra</label>
<input type="checkbox" name="..." value="AE" /><label for="...">United Arab Emirates</label>
<input type="checkbox" name="..." value="AF" /><label for="...">Afghanistan</label>

I would like each option displayed on it's own line in the browser, instead of all in a row. How can I inject html around or between the options? Or is there a CSS method to accomplish this?

Thanks.

Answer

Jakub Zalas picture Jakub Zalas · Dec 20, 2011

From The Symfony Docs

What you basically need to do is to overload checkbox_widget block.

{% form_theme form _self %}

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

Of course you can place your widgets in its own line with CSS (but it's not a Symfony2 question).