Simple_form: unchecked: false does not work when `config.boolean_style = :inline`

Created on 11 Nov 2015  路  6Comments  路  Source: heartcombo/simple_form

Notice the in the code samples below how the hidden field is not generated when using the nested boolean style.

Code generated when using the default config.boolean_style = :nested

<div class="checkbox">
  <input value="false" type="hidden" name="meal[completed]">
  <label class="boolean optional" for="meal_completed">
  <input class="boolean optional" type="checkbox" value="true" name="meal[completed]" id="meal_completed">Completed</label>
</div>

Code generated when using the default config.boolean_style = :inline

<div class="checkbox">
  <input class="boolean optional" type="checkbox" value="true" checked="checked" name="meal[completed]" id="meal_completed">
  <label class="boolean optional" for="meal_completed">Completed</label>
</div>

Expected Code generated when using the default config.boolean_style = :inline

<div class="checkbox">
  <input value="false" type="hidden" name="meal[completed]">
  <input class="boolean optional" type="checkbox" value="true" checked="checked" name="meal[completed]" id="meal_completed">
  <label class="boolean optional" for="meal_completed">Completed</label>
</div>

Most helpful comment

I've found a work around!

Example
= f.input :completed, as: :boolean, checked_value: true, unchecked_value: false

Work Around: change the booleans from ruby true and false to strings 'true' and 'false'
= f.input :completed, as: :boolean, checked_value: 'true', unchecked_value: 'false'

All 6 comments

Update: This bug only appears if you have custom value for unchecked_value. In this case unchecked_value: false

Example
= f.input :completed, as: :boolean, checked_value: true, unchecked_value: false

I've found a work around!

Example
= f.input :completed, as: :boolean, checked_value: true, unchecked_value: false

Work Around: change the booleans from ruby true and false to strings 'true' and 'false'
= f.input :completed, as: :boolean, checked_value: 'true', unchecked_value: 'false'

I'm experiencing the same issue without having custom checked/unchecked values. It's only when using boolean_style: :inline that it drops the unchecked value.

My workaround is to put a hidden field before the checkbox to make sure the unchecked value is included:

= f.input :my_boolean_column, as: :hidden, input_html: { value: '0', id: '' }
= f.input :my_boolean_column

Wow banged my head for 2 hrs over this. Seems to be fixed in 2.1.3 the latest/last version for rails 3 you can use. I was using simple_form 2.1.1 where I was having this issue. Could probably close this @josevalim @rafaelfranca

The bug here is that the nested boolean generates the hidden field when unchecked_value is falsey - that deviates from rails behavior since at least version 3.2 (see here: https://github.com/rails/rails/blob/4-2-stable/actionview/lib/action_view/helpers/tags/check_box.rb#L59)

So the fix would be not to create the hidden field when using the nested boolean along with unchecked_value: false. That way nested and inline booleans would behave the same. (If you want to create the hidden field nonetheless you could use unchecked_value: "false").

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gmrash picture gmrash  路  3Comments

mszyndel picture mszyndel  路  5Comments

mmhan picture mmhan  路  4Comments

tetherit picture tetherit  路  4Comments

frankyston picture frankyston  路  4Comments