Symfony2 - 如何将复选框/收音机的标签和输入放在同一行? [英] Symfony2 - How to put label and input for checkboxes/radios in a same line?
问题描述
在我的表单中,我有一些复选框,但默认情况下,我有 :
- 第一个收音机小部件
- 第一个标签
- 第二个收音机小部件
- 标签
这里是SYmfony2生成的html代码:
<输入...><label ...></label><输入...><label ...></label>
我想要的是:
第一个收音机小部件第一个标签
第二个收音机小部件第二个标签
html 代码是:
我想我必须覆盖choice_widget但不知道如何将输入和标签放在同一行
这是我可能需要覆盖的choice_widget:
{% block choice_widget %}{% 无空间 %}{% 如果展开 %}<div {{ block('widget_container_attributes') }}>{% for child in form %}{{ form_widget(child) }} {{ form_label(child) }}{% 结束为 %}
{% 别的 %}<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>{% 如果 empty_value 不是 none %}<option value="">{{ empty_value|trans }}</option>{% 万一 %}{% if preferred_choices|length >0%}{% 设置选项 = preferred_choices %}{{ 块('widget_choice_options')}}{% if options|length >0 和分隔符不是无 %}<option disabled="disabled">{{ 分隔符 }}</option>{% 万一 %}{% 万一 %}{% 设置选项 = 选择 %}{{ 块('widget_choice_options')}}</选择>{% 万一 %}{% endspaceless %}{% endblock choice_widget %}
我遇到了同样的问题,我无法找到解决方案,所以我自己解决了.您确实需要覆盖 {% block choice_widget %}
块,这是正确的.第一步是从打印出单独标签的 {% if expand %}
部分中删除 {{ form_label(child) }}
行.
{% block choice_widget %}{% 无空间 %}{% 如果展开 %}<div {{ block('widget_container_attributes') }}>{% for child in form %}{{ form_widget(child) }}{# {{ form_label(child) }} <--------------------- 删除这一行 #}{% 结束为 %}
{% 别的 %}<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>{% 如果 empty_value 不是 none %}<option value="">{{ empty_value|trans }}</option>{% 万一 %}{% if preferred_choices|length >0%}{% 设置选项 = preferred_choices %}{{ 块('widget_choice_options')}}{% if options|length >0 和分隔符不是无 %}<option disabled="disabled">{{ 分隔符 }}</option>{% 万一 %}{% 万一 %}{% 设置选项 = 选择 %}{{ 块('widget_choice_options')}}</选择>{% 万一 %}{% endspaceless %}{% endblock choice_widget %}
现在您只需要在 {% block checkbox_widget %}
块中处理打印标签.
{% 阻止 checkbox_widget %}{% 无空间 %}<label for="{{ id }}"><input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% 如果选中 %} checked="checked"{% endif %}/>{{ label|trans }}</label>{% endspaceless %}{% endblock checkbox_widget %}
你需要对 {% block radio_widget %}
做同样的事情,因为它现在没有标签.
{% 阻止 radio_widget %}{% 无空间 %}<label for="{{ id }}"><input type="radio" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% 如果选中 %} checked="checked"{% endif %}/>{{ label|trans }}</label>{% endspaceless %}{% endblock radio_widget %}
In my form I have some checkboxes, but by default, I have :
- the first radio widget
- the first label
- the second radio widget
- the label
Here is the html code generated by SYmfony2 :
<div>
<input ...>
<label ...></label>
<input ...>
<label ...></label>
</div>
What I want is to have :
the first radio widget the first label
the second radio widget the second label
The html code would be :
<label .....><input ....></label>
I think I have to override the choice_widget but don't know how to put input and label on the same line
Here is the choice_widget I may need to override :
{% 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 %}
I had the same problem, and I was unable to find a solution so I worked it out on my own. You are correct that you do need to override the {% block choice_widget %}
block. The first step is to remove the {{ form_label(child) }}
line from the {% if expanded %}
section that prints out a separate label.
{% 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 %}
Now you will just need to handle printing the label in the {% block checkbox_widget %}
block.
{% 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 %}
You will need to do the same for {% block radio_widget %}
since it would not otherwise have a label now.
{% 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 %}
这篇关于Symfony2 - 如何将复选框/收音机的标签和输入放在同一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!