有没有办法在symfony2中输入标签?

hel*_*bal 7 forms label symfony twig

关于symfony2表单组件及其模板的问题:

我有一堆样式的复选框(在一个表格行中大约10个).通常我会以<label>这种方式使用标记:<label><input/> some text</label>但我找不到在表单模板中更改它的方法(form_div_layout.html.twig).我甚至找不到一种方法来包装输入小部件及其标签周围的任何标签,我总是最终得到这样的标记:<input/> <some_tag><label>some text</label></some_tag>或者<some_tag><input/></some_tag> <label>some text</label>说它不是很有用,至少可以说......

谷歌搜索了一下,但找不到答案.

Bob*_* F. 5

Whistlegreg是正确的,你需要覆盖树枝表单模板.但是,您不需要删除{% block generic_label %}块.如果您需要有关如何覆盖模板的帮助,请参阅他的答案.

为了使用标签包装复选框输入标签,第一步将覆盖该{% block choice_widget %}块并{{ form_label(child) }}{% if expanded %}打印出单独标签的部分中删除该行.

{% block choice_widget %}
{% spaceless %}
    {% if expanded %}
        <div {{ block('widget_container_attributes') }}>
        {% for child in form %}
            {{ form_widget(child) }}
        {#  {{ form_label(child) }} <--------------------- remove this line #}  
        {% endfor %}
        </div>
    {% else %}
    <select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
        {% if empty_value is not none %}
            <option value="">{{ empty_value|trans }}</option>
        {% endif %}
        {% if preferred_choices|length > 0 %}
            {% set options = preferred_choices %}
            {{ block('widget_choice_options') }}
            {% if choices|length > 0 and separator is not none %}
                <option disabled="disabled">{{ separator }}</option>
            {% endif %}
        {% endif %}
        {% set options = choices %}
        {{ block('widget_choice_options') }}
    </select>
    {% endif %}
{% endspaceless %}
{% endblock choice_widget %}
Run Code Online (Sandbox Code Playgroud)

现在您只需要处理{% block checkbox_widget %}块中的标签打印.

{% 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|trans }}</label>
{% endspaceless %}
{% endblock checkbox_widget %}
Run Code Online (Sandbox Code Playgroud)

你需要做同样的事情,{% block radio_widget %}因为它现在不会有标签.

{% block radio_widget %}
{% spaceless %}
    <label  for="{{ id }}"><input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|trans }}</label>
{% endspaceless %}
{% endblock radio_widget %}
Run Code Online (Sandbox Code Playgroud)


gre*_*reg 4

我认为这就是您正在寻找的: http://symfony.com/doc/current/book/templatating.html#overriding-bundle-templates

您可以通过在 app/resources 文件夹中创建另一个同名文件来覆盖任何默认的树枝模板。

在您的情况下,您想要覆盖 form_div_layout.html.twig 模板,将其从捆绑包复制到 app/Resources/TwigBundle/views/Form/form_div_layout.html.twig,进行自定义,symfony 将使用该模板而不是默认值。

编辑:一旦您覆盖了模板,您就可以修改{% block checkbox_widget %}以使用 twig vars 将输入包裹在标签标签中

<label{% for attrname,attrvalue in attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
  {{label|trans }} 
  <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} /> 
</label> 
Run Code Online (Sandbox Code Playgroud)

您还需要删除“generic_label”定义,这意味着所有其他块都需要修改。