From 3d1095563c7d6f3e32bddbf9008607d921061e58 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 2 Feb 2012 09:23:07 -0200 Subject: Replicate :form html5 attribute to hidden field for check_box When the new html5 attribute :form is given to the check_box helper, it should be replicated to the hidden field as well. Closes #4848 --- actionpack/CHANGELOG.md | 3 +++ actionpack/lib/action_view/helpers/tags/check_box.rb | 6 +++++- actionpack/test/template/form_helper_test.rb | 13 ++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 0dafef90ae..38d5f0b4e5 100644 --- a/actionpack/CHANGELOG.md +++ b/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* diff --git a/actionpack/lib/action_view/helpers/tags/check_box.rb b/actionpack/lib/action_view/helpers/tags/check_box.rb index 7ad5de0596..579cdb9fc9 100644 --- a/actionpack/lib/action_view/helpers/tags/check_box.rb +++ b/actionpack/lib/action_view/helpers/tags/check_box.rb @@ -25,7 +25,7 @@ module ActionView 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 @@ -48,6 +48,10 @@ module ActionView 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 diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 4e440c6a13..9366960caa 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -421,6 +421,13 @@ class FormHelperTest < ActionView::TestCase ) end + def test_checkbox_form_html5_attribute + assert_dom_equal( + '', + check_box("post", "secret", :form => "new_form") + ) + end + def test_radio_button assert_dom_equal('', radio_button("post", "title", "Hello World") @@ -2168,8 +2175,8 @@ class FormHelperTest < ActionView::TestCase end protected - def protect_against_forgery? - false - end + def protect_against_forgery? + false + end end -- cgit v1.2.3