Permalink
Browse files

Attempts to fix GH-264 by rendering our own hidden_field_tag outside …

…the label.

We render our own check_box_tag inside the label as well, rather than leaning on Rails' check_box() helper which would render both inside the label, which is invalid HTML5
  • Loading branch information...
justinfrench committed Oct 12, 2010
1 parent 41b7b3b commit 0f88cb3a19f32b7c1dc123c083a269289170f6de
Showing with 14 additions and 7 deletions.
  1. +9 −5 lib/formtastic.rb
  2. +5 −2 spec/inputs/boolean_input_spec.rb
View
@@ -1227,21 +1227,25 @@ def country_input(method, options)
# Outputs a label containing a checkbox and the label text. The label defaults
# to the column name (method name) and can be altered with the :label option.
# :checked_value and :unchecked_value options are also available.
- #
- # TODO: doesn't look to the model value to set html_options[:checked]
def boolean_input(method, options)
html_options = options.delete(:input_html) || {}
checked_value = options.delete(:checked_value) || '1'
unchecked_value = options.delete(:unchecked_value) || '0'
- input = self.check_box(method, strip_formtastic_options(options).merge(html_options),
- checked_value, unchecked_value)
+ #input = self.check_box(method, strip_formtastic_options(options).merge(html_options),
+ # checked_value, unchecked_value)
+ input = template.check_box_tag(
+ "#{@object_name}[#{method}]",
+ checked_value,
+ (@object && @object.send(:"#{method}") == checked_value),
+ :id => "#{@object_name}_#{method}"
+ )
options = options_for_label(options)
# the label() method will insert this nested input into the label at the last minute
options[:label_prefix_for_nested_input] = input
- self.label(method, options)
+ template.hidden_field_tag(method, unchecked_value) << self.label(method, options)
end
# Generates an input for the given method using the type supplied with :as.
@@ -23,7 +23,9 @@
output_buffer.should have_tag('form li label', :count => 1)
output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
output_buffer.should have_tag('form li label', /Allow comments/)
- output_buffer.should have_tag('form li label input[@type="checkbox"]')
+ output_buffer.should have_tag('form li label input[@type="checkbox"]', :count => 1)
+ output_buffer.should have_tag('form li input[@type="hidden"]', :count => 1)
+ output_buffer.should_not have_tag('form li label input[@type="hidden"]', :count => 1) # invalid HTML5
end
it 'should generate a checkbox input' do
@@ -42,7 +44,8 @@
output_buffer.concat(form) if Formtastic::Util.rails3?
output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]:not([@unchecked_value][@checked_value])')
- output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
+ output_buffer.should have_tag('form li input[@type="hidden"][@value="unchecked"]')
+ output_buffer.should_not have_tag('form li label input[@type="hidden"]') # invalid HTML5
end
it 'should generate a label and a checkbox even if no object is given' do

0 comments on commit 0f88cb3

Please sign in to comment.