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

后端 未结 9 1804
一向
一向 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 10:51

    When a label is rendered it will include a for attribute - this links the label to the input - see the docs on label here changing the output to what you suggested is just another way of linking the label and the input

    If you want the label to display to the left of the input you need to change your template to :

    {% for child in form %}
       
    {{ form_label(child) }} {{ form_widget(child) }}
    {% endfor %}

    Which renders the label before the input and then this creates the following output

    You can then apply some CSS styling to make it display inline :

    ​div label {
        display: inline-block;
        width: 200px;
    }​
    

    By default - without any CSS the label would line up before the input with this HTML layout - but the inline-block allows you to also use the width attribute to ensure all fields are lined up correctly - irrespective of the length of label text

    Working example here

提交回复
热议问题