可以为特定表单小部件类型的行定义块,例如{% block checkbox_row %}
. 我在这里发现了这个:http: //forum.symfony-project.org/viewtopic.php?f=23 &t=57986#p153546
然后将标签包裹在复选框的小部件周围所需的所有内容如下:
{% block checkbox_row %}
{% spaceless %}
<div>
{% 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 %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}
{{ form_widget(form) }}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
</label>
{{ form_errors(form) }}
</div>
{% endspaceless %}
{% endblock checkbox_row %}
标签代码已从 复制并粘贴{% block form_label %}
。当我使用 Zurb 的 Foundation 框架时,我已将表单错误放在小部件下方。
单选按钮的代码更复杂,因为{% block radio_row %}
它似乎不存在,所以你必须接受 Whistlegreg 的建议并编辑该{% block choice_widget %}
块,在 Symfony 2.1 中这实际上是现在的{% block choice_widget_expanded %}
. 这是我的代码:
{% block choice_widget_expanded %}
{% spaceless %}
<div {{ block('widget_container_attributes') }}>
{% 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 %}
<label{% for attrname, attrvalue in child_label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
{% 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) }}
</label>
{% endif %}
{% endfor %}
</div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
在 Symfony 2.1.9 DEV 中测试和工作。