Symfony2 - How to put label and input for checkboxes/radios in a same line?

后端 未结 9 1790
一向
一向 2020-12-13 10:25

In my form I have some checkboxes, but by default, I have :

  • the first radio widget
  • the first label
  • the second radio widget
  • th
9条回答
  •  遥遥无期
    2020-12-13 11:02

    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 %}
            
    {% for child in form %} {{ form_widget(child) }} {# {{ form_label(child) }} <--------------------- remove this line #} {% endfor %}
    {% else %} {% 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 %}
        
    {% 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 %}
        
    {% endspaceless %}
    {% endblock radio_widget %}
    

提交回复
热议问题