Symfony2 - 如何将复选框/无线电的标签和输入放在同一行?

Rev*_*air 19 php symfony twig

在我的表单中,我有一些复选框,但默认情况下,我有:

  • 第一个无线电小部件
  • 第一个标签
  • 第二个无线电小工具
  • 标签

这是SYmfony2生成的html代码:

  <div>
    <input ...>
    <label ...></label>
    <input ...>
    <label ...></label>
  </div>
Run Code Online (Sandbox Code Playgroud)

什么我想是有:

第一无线电窗口小部件的第一个标签
的第二无线电窗口小部件的第二个标签

HTML代码将是:

  <label .....><input ....></label>
Run Code Online (Sandbox Code Playgroud)

我想我必须覆盖choice_widget,但不知道如何将输入和标签放在同一行

这是我可能需要覆盖的choice_widget:

    {% block choice_widget %}
        {% spaceless %}
            {% if expanded %}
                <div {{ block('widget_container_attributes') }}>
                   {% for child in form %}
                      {{ form_widget(child) }}  {{ form_label(child) }}
                   {% 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)

Bob*_* F. 30

我有同样的问题,我无法找到解决方案,所以我自己解决了.你确实需要覆盖{% 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)

  • 不适用于Symfony 2.3,因为choice_widget已分为choice_widget_expanded和choice_widget_collapsed.如果您逐字遵循此建议,将影响选择的行为.Se bellow更新解决方案. (4认同)
  • 不知道为什么这会被投票,因为它产生2.0.x上请求的输出 (2认同)

Xav*_*r13 13

在Alberto Gaona和James的帮助下,Symfony 2.4正确的解决方案集成了BS3收音机和复选框如下:

在您看来,您有:

{% form_theme form 'AcmeDemoBundle:Form:fields.html.twig' %}

// A radio or checkbox input
{{ form_widget(form.example) }}
Run Code Online (Sandbox Code Playgroud)

然后在您的fields.html.twig中(覆盖https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig ;请参阅http: //symfony.com/doc/current/cookbook/form/form_customization.html)

{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {% if multiple %}
            <div class="checkbox">
        {% else %}
            <div class="radio">
        {% endif %}

        {{ form_widget(child) }}
        </div>
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

{% block checkbox_widget %}
{% spaceless %}
{% if label is empty %}
    {% set label = name|humanize %}
{% endif %}
    <label  for="{{ id }}">
        <input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %} />{{ label|trans({}, translation_domain) }}
    </label>
{% endspaceless %}
{% endblock checkbox_widget %}

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


Alb*_*ona 11

注意:更新了Symfony 2.3的Bob F解决方案(请参阅https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig):

覆盖choice_widget_expanded:

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
    {% for child in form %}
        {{ form_widget(child) }}
    {% endfor %}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}
Run Code Online (Sandbox Code Playgroud)

覆盖复选框(引导样式):

{% block checkbox_widget %}
{% spaceless %}
    <label  for="{{ id }}" class="checkbox {% if checked %}checked{% endif %}" ><span class="icons"><span class="first-icon fui-checkbox-unchecked"></span><span class="second-icon fui-checkbox-checked"></span></span><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)

覆盖radiobutton

{% 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)

  • 将这些块放在一个单独的文件中,在字段上使用form_widget之前,您可以使用{%form_theme form.field':: theme.html.twig'%}来使用它.花了一些时间来弄清楚如何在一个领域实际使用这些:) (2认同)