Simple_form: Even with :label => false, label tag is added (:as => boolean)

Created on 28 Mar 2012  路  12Comments  路  Source: heartcombo/simple_form

Starting with simple_form 2.0, the :label => false is not honored properly:

 %td= rif.input :_destroy,  :as => :boolean, :label => false

yields

<td>
<div class="input boolean optional">
  <input name="order_request[requested_items_attributes][0][_destroy]" type="hidden" value="0">
  <label class="check_box">
    <input class="boolean optional" id="order_request_requested_items_attributes_0__destroy" name="order_request[requested_items_attributes][0][_destroy]" type="checkbox" value="1">
  </label>
</div>
</td>

Since my input is in a table, the label takes useless space in the table. Anyway, the label tag shouldn't be here at the first place.

Most helpful comment

So, using :label => false actually doesn't add up the very outer label component, that's ok. The nested_style option is supposed to work that way, wrapping the boolean with an inner label, to mimic the functionality required by bootstrap to work. If you're not using bootstrap, you may want to change it to :inline if you don't want that wrapping label.

I _think_ that this would probably work for you:

rif.input :_destroy, :as => :boolean, :boolean_style => :inline, :label => false

Do you have any issues with this setup?

All 12 comments

The label that is skipped is the label that goes alongside with with all inputs, not the label wrapping each checkbox. If you want a different output, i think you will have to generate the checkboxes on your own (using f.check_box).

@josevalim nope, it's the very label. If I remove the :label => false, I get:

<div class="input boolean optional">
  <input name="order_request[requested_items_attributes][1][_destroy]" type="hidden" value="0">
   <label class="boolean optional control-label checkbox" for="order_request_requested_items_attributes_1__destroy">
    <input class="boolean optional" id="order_request_requested_items_attributes_1__destroy" name="order_request[requested_items_attributes][1][_destroy]" type="checkbox" value="1">  destroy
   </label>
</div>

I also tried to add style or class to the label with (:label_html => {:style => "width: auto;", :class => 'no_width'}). It is properly applied when label is showing but it is scrapped with :label => false* showing clearly that is the simple_form element label

Reopening then, thanks for the follow up. :)

Glad I can help, I love the gem.

For a workaround:
%td= rif.input :_destroy, :as => :boolean, :label => '', :label_html => {:style => "width: auto; min-width:0;"}

Seems weird that label: false does not work, SimpleForm checks for that =(. Can you post the wrapper you're using in your config please? Also, which boolean_style option you have in your initializer?

According to your output, I suppose it's using the nested style due to the label / checkbox, as @josevalim pointed out, but lets check first. Thanks.

Ok, here are my findings:
simple_form is in :nested style. I think the bug is here: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/boolean_input.rb#L5-9, even though label_input method doesn't add the label, the input method (in nested style) will still add it, ignoring the requested option. There should be an if option[:label] == false again there to not add the label tag if needed.

In :inline style, the label tag is properly suppressed with :label => false. However, my workaround doesn't work any more :o) and :label => '' is somewhat stripped so the default _destroy label appears.

So, using :label => false actually doesn't add up the very outer label component, that's ok. The nested_style option is supposed to work that way, wrapping the boolean with an inner label, to mimic the functionality required by bootstrap to work. If you're not using bootstrap, you may want to change it to :inline if you don't want that wrapping label.

I _think_ that this would probably work for you:

rif.input :_destroy, :as => :boolean, :boolean_style => :inline, :label => false

Do you have any issues with this setup?

@carlosantoniodasilva It works perfectly, thanks!

(sorry for the late answer, I somewhat didn't get the notification email...)

@gamov great, thanks for reporting back :)

I've been hit by this again today. I think it's counterintuitive to set label: false and still have a label element produced by simple_form

That generated label is merely for formatting purposes and required by bootstrap. I'm not sure we shout change to remove it if you give label: false.

Not always does the label go around the input for formatting. In many instances, the label wraps both an input and a string of text. That's where I got stuck.

Was this page helpful?
0 / 5 - 0 ratings