Skip to content

Commit

Permalink
Replicate :form html5 attribute to hidden field for check_box
Browse files Browse the repository at this point in the history
When the new html5 attribute :form is given to the check_box helper, it
should be replicated to the hidden field as well. Closes rails#4848
  • Loading branch information
carlosantoniodasilva committed Feb 2, 2012
1 parent 593fe43 commit 3d10955
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,5 +1,8 @@
## Rails 4.0.0 (unreleased) ##

* check_box with `:form` html5 attribute will now replicate the `:form`
attribute to the hidden field as well. *Carlos Antonio da Silva*

* `label` form helper accepts :for => nil to not generate the attribute. *Carlos Antonio da Silva*

* Add `:format` option to number_to_percentage *Rodrigo Flores*
Expand Down
6 changes: 5 additions & 1 deletion actionpack/lib/action_view/helpers/tags/check_box.rb
Expand Up @@ -25,7 +25,7 @@ def render
add_default_name_and_id(options)
end

hidden = @unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => @unchecked_value, "disabled" => options["disabled"]) : "".html_safe
hidden = hidden_field_for_checkbox(options)
checkbox = tag("input", options)
hidden + checkbox
end
Expand All @@ -48,6 +48,10 @@ def checked?(value)
value.to_i != 0
end
end

def hidden_field_for_checkbox(options)
@unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
end
end
end
end
Expand Down
13 changes: 10 additions & 3 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -421,6 +421,13 @@ def test_checkbox_disabled_disables_hidden_field
)
end

def test_checkbox_form_html5_attribute
assert_dom_equal(
'<input form="new_form" name="post[secret]" type="hidden" value="0" /><input checked="checked" form="new_form" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret", :form => "new_form")
)
end

def test_radio_button
assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
radio_button("post", "title", "Hello World")
Expand Down Expand Up @@ -2168,8 +2175,8 @@ def test_fields_for_returns_block_result
end

protected
def protect_against_forgery?
false
end

def protect_against_forgery?
false
end
end

0 comments on commit 3d10955

Please sign in to comment.