2

我已经在我的 symfony 应用程序中覆盖了我的表单的 Twig 模板,这样我就可以更好地控制复选框的标签。

但是,我遇到了复选框标签的问题。

这是我在自定义模板文件中覆盖的代码:

{# Labels #}
{% block form_label %}
    {% spaceless %}
        {% if label is not sameas(false) %}
            {% 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 %}
            {% if label is empty %}
                {% set label = name|humanize %}
            {% endif %}
        {% endif %}
        {% if 'checkbox' not in block_prefixes %}
             <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
        {% endif %}
    {% endspaceless %}
{% endblock form_label %}

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

它工作正常,但我无法让复选框工作的标签文本节点。

当我有一个复选框时,它会生成如下内容:

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

它应该在哪里:

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

关于如何在复选框后出现标签字符串的任何线索?

编辑:

我找到了一个解决方案,效果很好,但我不确定它是否是最好的。

{% block form_row %}
{% spaceless %}
    <div>
        {{ form_errors(form) }}
        {% if 'checkbox'  in block_prefixes %}
            {{ form_widget(form) }}
            {{ form_label(form) }}
        {% elseif 'radio'  in block_prefixes %}
            {{ form_widget(form) }}
            {{ form_label(form) }}
        {% else %}
            {{ form_label(form) }}
            {{ form_widget(form) }}
        {% endif %}

    </div>
{% endspaceless %}
{% endblock form_row %}
4

1 回答 1

3

您可以删除标签{% block choice_widget %}以防止它出现在默认位置。

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
            {{ form_label(child) }}             {# HERE #}  
        {% endfor %}
        </div>
    {% else %}
//....
{% endspaceless %}
{% endblock choice_widget %}

如果你这样做,你也必须覆盖{% block radio_widget %}。否则它不会有标签。

<label  for="{{ id }}"><input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|trans }}</label>

然后,您可以删除{% if 'checkbox' not in block_prefixes %}您放入的行{% block form_label %}

它对我有用。

Symfony2 - 如何将复选框/收音机的标签和输入放在同一行?

编辑 :

似乎他们{% block choice_widget %}在 2.3.3 中拆分了。您现在必须编辑choice_expanded 块,并如上所述删除标签行。

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
        {{ form_label(child) }}           {# There #}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
于 2013-08-10T16:17:52.490 回答