Symfony2-如何将复选框和收音机的标签和输入放在同一行? [英] Symfony2 - How to put label and input for checkboxes/radios in a same line?
问题描述
在我的表单中,我有一些复选框, 但默认情况下,我有:
In my form I have some checkboxes, but by default, I have :
- 第一个电台小部件
- 第一个标签
- 第二个收音机小部件
- 标签
这是SYmfony2生成的html代码:
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
html代码为:
<label .....><input ....></label>
我认为我必须重写choice_widget,但不知道如何将输入和标签放在同一行上
I think I have to override the choice_widget but don't know how to put input and label on the same line
这是我可能需要覆盖的choice_widget:
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 %}
推荐答案
我遇到了同样的问题,并且找不到解决方案,所以我自己解决了这个问题.您确实需要覆盖{% block choice_widget %}
块,这是正确的.第一步是从打印单独标签的{% if expanded %}
部分中删除{{ form_label(child) }}
行.
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 %}
现在,您只需要处理在{% block checkbox_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 %}
您将需要对{% block radio_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屋!