diff --git a/CHANGELOG.md b/CHANGELOG.md index c0e158714..c222065d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,9 @@ * Add possibility to give a block to `collection_radio` and `collection_check_boxes`, to generate custom label and input structure. It is used internally with the :nested option for `:boolean_style`, and is useful to allow some more customization if required. + * Do not generate hidden check box field when using nested boolean style, as it is considered + invalid markup in HTML5. This will only work in Rails > 3.2.1 (not released at this time). + More info in [#215](https://github.com/plataformatec/simple_form/issues/215) ### deprecation * Deprecate the `translate` configuration in favor of `translate_labels` diff --git a/lib/simple_form/inputs/boolean_input.rb b/lib/simple_form/inputs/boolean_input.rb index cec5bc25a..b8efc17c3 100644 --- a/lib/simple_form/inputs/boolean_input.rb +++ b/lib/simple_form/inputs/boolean_input.rb @@ -2,14 +2,14 @@ module SimpleForm module Inputs class BooleanInput < Base def input - @builder.check_box(attribute_name, input_html_options) + build_check_box end def label_input if options[:label] == false input elsif nested_boolean_style? - @builder.label(label_target, label_html_options) { input + label_text } + @builder.label(label_target, label_html_options) { build_check_box(nil) + label_text } else input + label end @@ -17,6 +17,13 @@ def label_input private + # Build a checkbox tag using default unchecked value. This allows us to + # reuse the method for nested boolean style, but with nil unchecked value, + # which won't generate the hidden checkbox (only in Rails > 3.2.1). + def build_check_box(unchecked_value='0') + @builder.check_box(attribute_name, input_html_options, '1', unchecked_value) + end + # Booleans are not required by default because in most of the cases # it makes no sense marking them as required. The only exception is # Terms of Use usually presented at most sites sign up screen. diff --git a/lib/simple_form/inputs/collection_check_boxes_input.rb b/lib/simple_form/inputs/collection_check_boxes_input.rb index 575712a8b..1a1c686bb 100644 --- a/lib/simple_form/inputs/collection_check_boxes_input.rb +++ b/lib/simple_form/inputs/collection_check_boxes_input.rb @@ -10,7 +10,7 @@ def has_required? end def nested_boolean_style_item_tag(value, html_options) - @builder.check_box(attribute_name, html_options, value) + @builder.check_box(attribute_name, html_options, value, nil) end end end