Simple_form: input_field for boolean generates more than just input

Created on 24 Sep 2013  路  5Comments  路  Source: heartcombo/simple_form

Using Rails 4.0.0 and SimpleForm 3.0.0

If in a form I use input_field on boolean attribute, input is wrapped in label. To my understanding it should not be.

Example code

<%= simple_form_for @door do |f| %>
  <%= f.input_field :locked %>
<% end %>

generated markup would be (unimportant parts skipped)

  <form class="simple_form form-vertical">

    <input name="door[locked]" type="hidden" value="0">
    <label class="checkbox">
      <input class="boolean optional" id="door_locked" name="door[locked]" type="checkbox" value="1">
    </label>

  </form>

If for the same field I change field type to, let's say :text, only text area input is generated (as expected).

Most helpful comment

Ok, so to make it work I used following (leaving as a reference for anyone having the same problem)

<%= simple_form_for @door do |f| %>
  <%= f.input_field :locked, boolean_style: :inline
  <%= f.label :locked %>
<% end %>

I think this behavior should be at least mentioned in README.
I searched code and it is not mentioned anywhere as well.

All 5 comments

This is how Simple Form works. If you don't want that label change the boolean_style to inline http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box

We choose to generate the input inside the label when the boolean_style is configured to :nester. You can remove that label using :inline value to that configuration.

This is by design.

Ok, so to make it work I used following (leaving as a reference for anyone having the same problem)

<%= simple_form_for @door do |f| %>
  <%= f.input_field :locked, boolean_style: :inline
  <%= f.label :locked %>
<% end %>

I think this behavior should be at least mentioned in README.
I searched code and it is not mentioned anywhere as well.

Yes, I did the same but I didn't found. I'll open an issue to document this behavior.

Thank you for pointing this :heart:

I understand this is by design. However, is it possible to configure this through Wrappers API?

b.wrapper tag: 'label', class: 'form-check-label' do |ba|
  ba.use :input, class: 'form-check-input', boolean_style: 'inline'
  ba.use :label_text
end

The above is not working. I'm not sure how to make it work. @rafaelfranca, Any idea?

Was this page helpful?
0 / 5 - 0 ratings