Skip to content

Commit

Permalink
Merge check box fixes from remote-tracking branch 'cantonio/checkbox-…
Browse files Browse the repository at this point in the history
…hidden-backport' into 3-2-stable
  • Loading branch information
josevalim committed Jan 27, 2012
2 parents 1c5bd8a + defb751 commit 2e5ec3b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
## Rails 3.2.2 (unreleased) ##

* check_box helper with :disabled => true will generate a disabled hidden field to conform with the HTML convention where disabled fields are not submitted with the form.
This is a behavior change, previously the hidden tag had a value of the disabled checkbox.
*Tadas Tamosauskas*

## Rails 3.2.1 (January 26, 2012) ##

* Documentation improvements.
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1092,7 +1092,7 @@ def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
else
add_default_name_and_id(options)
end
hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
hidden = unchecked_value ? tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value, "disabled" => options["disabled"]) : ""
checkbox = tag("input", options)
(hidden + checkbox).html_safe
end
Expand Down
15 changes: 11 additions & 4 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -373,6 +373,14 @@ def test_check_box_with_explicit_checked_and_unchecked_values
)
end

def test_check_box_with_nil_unchecked_value
@post.secret = "on"
assert_dom_equal(
'<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
check_box("post", "secret", {}, "on", nil)
)
end

def test_check_box_with_multiple_behavior
@post.comment_ids = [2,3]
assert_dom_equal(
Expand All @@ -385,11 +393,10 @@ def test_check_box_with_multiple_behavior
)
end


def test_checkbox_disabled_still_submits_checked_value
def test_checkbox_disabled_disables_hidden_field
assert_dom_equal(
'<input name="post[secret]" type="hidden" value="1" /><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret", { :disabled => :true })
'<input name="post[secret]" type="hidden" value="0" disabled="disabled"/><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
check_box("post", "secret", { :disabled => true })
)
end

Expand Down

0 comments on commit 2e5ec3b

Please sign in to comment.