Skip to content

Commit

Permalink
Merge pull request #1376 from capripot/add_hidden_input_option_to_boo…
Browse files Browse the repository at this point in the history
…lean

Add `hidden_input` option to checkbox
  • Loading branch information
rafaelfranca authored Jul 13, 2016
2 parents 409c85e + c2dd12c commit 151acb0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/simple_form/inputs/boolean_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def build_check_box_without_hidden_field(options)
# we need the hidden field to be *outside* the label (otherwise it
# generates invalid html - html5 only).
def build_hidden_field_for_checkbox
return "" unless include_hidden?
options = { value: unchecked_value, id: nil, disabled: input_html_options[:disabled] }
options[:name] = input_html_options[:name] if input_html_options.has_key?(:name)

Expand All @@ -81,6 +82,10 @@ def required_by_default?
false
end

def include_hidden?
options.fetch(:include_hidden, true)
end

def checked_value
options.fetch(:checked_value, '1')
end
Expand Down
8 changes: 8 additions & 0 deletions test/inputs/boolean_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ class BooleanInputTest < ActionView::TestCase
end
end

test 'input with nested style allows disabling hidden field' do
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :boolean, include_hidden: false
assert_select "label.boolean > input.boolean"
assert_no_select "input[type=hidden] + label.boolean"
end
end

test 'input boolean works using :input only in wrapper config (no label_input)' do
swap_wrapper do
with_input_for @user, :active, :boolean
Expand Down

0 comments on commit 151acb0

Please sign in to comment.