Skip to content

Commit

Permalink
Do not generate hidden check box field when using nested boolean style
Browse files Browse the repository at this point in the history
It is considered invalid markup in HTML5. This will only work in
Rails > 3.2.1 (not released at this time). If you really care that much
about this, please use Rails 3-2-stable branch for now, that contains a
backported fix from master.

Backport commit: rails/rails@2e5ec3b
More info in #215
  • Loading branch information
carlosantoniodasilva committed Jan 27, 2012
1 parent 87ce700 commit 0b34519
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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`
Expand Down
11 changes: 9 additions & 2 deletions lib/simple_form/inputs/boolean_input.rb
Expand Up @@ -2,21 +2,28 @@ 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
end

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.
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_form/inputs/collection_check_boxes_input.rb
Expand Up @@ -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
Expand Down

0 comments on commit 0b34519

Please sign in to comment.