Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure that the empty default value for check_boxes collections is…

… passed as an array value

The current way of sending the value as string can trigger a TypeError when Rack validates
the input field and one or more check_boxes have been ticked.

See rails issue #3030 for some more background information.

This changes the variable type used for commit 96a2e54
  • Loading branch information...
commit 7a5120edbd4db386c98d9dd6a903a1b927951e62 1 parent 5156877
@yabawock yabawock authored
Showing with 16 additions and 4 deletions.
  1. +9 −2 lib/formtastic.rb
  2. +7 −2 spec/inputs/check_boxes_input_spec.rb
View
11 lib/formtastic.rb
@@ -1168,7 +1168,7 @@ def check_boxes_input(method, options)
li_options = value_as_class ? { :class => [method.to_s.singularize, 'default'].join('_') } : {}
list_item_content = []
- list_item_content << template.content_tag(:li, self.hidden_field(input_name, {:value => ''}), li_options) unless hidden_fields
+ list_item_content << self.create_hidden_field_for_check_boxes(input_name, value_as_class) unless hidden_fields
list_item_content << collection.map do |c|
label = c.is_a?(Array) ? c.first : c
value = c.is_a?(Array) ? c.last : c
@@ -1217,9 +1217,16 @@ def find_selected_values_for_column(method, options)
selected_values
end
+ # Outputs a custom hidden field for check_boxes
+ def create_hidden_field_for_check_boxes(method, value_as_class) #:nodoc:
+ li_options = value_as_class ? { :class => [method.to_s.singularize, 'default'].join('_') } : {}
+ input_name = "#{object_name}[#{method.to_s}][]"
+ template.content_tag(:li, template.hidden_field_tag(input_name, ''), li_options)
+ end
+
# Outputs a checkbox tag. If called with no_hidden_input = true a plain check_box_tag is returned,
# otherwise the helper uses the output generated by the rails check_box method.
- def create_check_boxes(input_name, html_options = {}, checked_value = "1", unchecked_value = "0", hidden_fields = false)
+ def create_check_boxes(input_name, html_options = {}, checked_value = "1", unchecked_value = "0", hidden_fields = false) #:nodoc:
return template.check_box_tag(input_name, checked_value, html_options[:checked], html_options) unless hidden_fields == true
self.check_box(input_name, html_options, checked_value, unchecked_value)
end
View
9 spec/inputs/check_boxes_input_spec.rb
@@ -75,9 +75,14 @@
end
end
- it 'should have a hidden field with an empty value for the collection to allow clearing of all checkboxes' do
+ it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
output_buffer.concat(@form) if Formtastic::Util.rails3?
- output_buffer.should have_tag("form li fieldset ol li input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
+ output_buffer.should have_tag("form li fieldset ol li input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
+ end
+
+ it 'should not have a hidden field with an empty string value for the collection' do
+ output_buffer.concat(@form) if Formtastic::Util.rails3?
+ output_buffer.should_not have_tag("form li fieldset ol li input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
end
it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
Please sign in to comment.
Something went wrong with that request. Please try again.