Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is possible to define blocks for the rows of specific form widget types, e.g. <code>{% block checkbox_row %}</code>. I discovered this here: <a href="http://forum.symfony-project.org/viewtopic.php?f=23&amp;t=57986#p153546" rel="nofollow">http://forum.symfony-project.org/viewtopic.php?f=23&amp;t=57986#p153546</a></p> <p>All that is then required to wrap the label around the widget for checkboxes is the following:</p> <pre><code>{% block checkbox_row %} {% spaceless %} &lt;div&gt; {% if not compound %} {% set label_attr = label_attr|merge({'for': id}) %} {% endif %} {% if required %} {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %} {% endif %} &lt;label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}&gt;{{ label|trans({}, translation_domain) }} {{ form_widget(form) }} {% if label is empty %} {% set label = name|humanize %} {% endif %} &lt;/label&gt; {{ form_errors(form) }} &lt;/div&gt; {% endspaceless %} {% endblock checkbox_row %} </code></pre> <p>The label code has been copied and pasted from <code>{% block form_label %}</code>. I've put the form error below the widget as I'm using Zurb's Foundation framework.</p> <p>The code for radio buttons, is more complicated because <code>{% block radio_row %}</code> doesn't appear to exist, so you have to take Whistlegreg's advice and edit the <code>{% block choice_widget %}</code> block, in Symfony 2.1 that's actually now the <code>{% block choice_widget_expanded %}</code>. Here's my code:</p> <pre><code>{% block choice_widget_expanded %} {% spaceless %} &lt;div {{ block('widget_container_attributes') }}&gt; {% for child in form %} {% set child_label = child.get('label') %} {% if child_label is not sameas(false) %} {% set child_id = child.get('id') %} {% set child_compound = child.get('compound') %} {% set child_required = child.get('required') %} {% set child_label_attr = child.get('label_attr') %} {% if not child_compound %} {% set child_label_attr = child_label_attr|merge({'for': child_id}) %} {% endif %} {% if child_required %} {% set child_label_attr = child_label_attr|merge({'class': (child_label_attr.class|default('') ~ ' required')|trim}) %} {% endif %} &lt;label{% for attrname, attrvalue in child_label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}&gt; {% endif %} {{ form_widget(child) }} {% if child_label is not sameas(false) %} {% if child_label is empty %} {% set child_label = name|humanize %} {% endif %} {{ child_label|trans({}, translation_domain) }} &lt;/label&gt; {% endif %} {% endfor %} &lt;/div&gt; {% endspaceless %} {% endblock choice_widget_expanded %} </code></pre> <p>Tested and working in Symfony 2.1.9 DEV.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload