Permalink
Browse files

Fix handling of radio buttons and checkboxes in the labelers

  • Loading branch information...
1 parent 246e091 commit d8bb95cfb8292650a5b5067d82812b6340df59cf @jeremyevans committed May 1, 2012
Showing with 26 additions and 6 deletions.
  1. +4 −0 CHANGELOG
  2. +12 −4 lib/forme.rb
  3. +8 −0 spec/forme_spec.rb
  4. +2 −2 spec/sequel_plugin_spec.rb
View
@@ -1,5 +1,9 @@
=== HEAD
+* Make explicit labeler put label after checkboxes and radio buttons instead of before (jeremyevans)
+
+* Make implicit labeler not include hidden checkbox inside label (jeremyevans)
+
* Recognize :cols and :rows options as attributes for textarea inputs in the default formatter (jeremyevans)
* Recognize :size and :maxlength options as attributes for text inputs in the default formatter (jeremyevans)
View
@@ -940,10 +940,14 @@ class Labeler
# the label occurs before the tag.
def call(tag, input)
label = input.opts[:label]
- t = if [:radio, :checkbox].include?(input.type)
- [tag, ' ', label]
+ if [:radio, :checkbox].include?(input.type)
+ if input.type == :checkbox && tag.is_a?(Array) && tag.length == 2 && tag.first.attr[:type].to_s == 'hidden'
+ return [tag.first , input.tag(:label, input.opts[:label_attr]||{}, [tag.last, ' ', label])]
+ else
+ t = [tag, ' ', label]
+ end
else
- [label, ": ", tag]
+ t = [label, ": ", tag]
end
input.tag(:label, input.opts[:label_attr]||{}, t)
end
@@ -963,7 +967,11 @@ class Labeler::Explicit
# :label_for option is used, the label created will not be
# associated with an input.
def call(tag, input)
- [input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]]), tag]
+ if [:radio, :checkbox].include?(input.type)
+ [tag, input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]])]
+ else
+ [input.tag(:label, {:for=>input.opts.fetch(:label_for, input.opts[:id])}.merge(input.opts[:label_attr]||{}), [input.opts[:label]]), tag]
+ end
end
end
View
@@ -196,6 +196,10 @@ def sel(opts, s)
@f.input(:text, :label=>'Foo', :value=>'foo', :label_attr=>{:class=>'bar'}).to_s.should == '<label class="bar">Foo: <input type="text" value="foo"/></label>'
end
+ specify "should handle implicit labels with checkboxes" do
+ @f.input(:checkbox, :label=>'Foo', :value=>'foo', :name=>'a').to_s.should == '<input name="a" type="hidden" value="0"/><label><input name="a" type="checkbox" value="foo"/> Foo</label>'
+ end
+
specify "should automatically note the input has errors if :error option is used" do
@f.input(:text, :error=>'Bad Stuff!', :value=>'foo').to_s.should == '<input class="error" type="text" value="foo"/><span class="error_message">Bad Stuff!</span>'
end
@@ -301,6 +305,10 @@ def sel(opts, s)
@f.input(:textarea, :labeler=>:explicit, :label=>'bar', :id=>:foo).to_s.should == '<label for="foo">bar</label><textarea id="foo"></textarea>'
end
+ specify "should handle explicit labels with checkboxes" do
+ @f.input(:checkbox, :labeler=>:explicit, :label=>'Foo', :value=>'foo', :name=>'a', :id=>'bar').to_s.should == '<input id="bar_hidden" name="a" type="hidden" value="0"/><input id="bar" name="a" type="checkbox" value="foo"/><label for="bar">Foo</label>'
+ end
+
specify "inputs should accept a :error_handler option to use a custom error_handler" do
@f.input(:textarea, :error_handler=>proc{|t, i| [t, "!!! #{i.opts[:error]}"]}, :error=>'bar', :id=>:foo).to_s.should == '<textarea class="error" id="foo"></textarea>!!! bar'
end
@@ -84,8 +84,8 @@
end
specify "should use a checkbox for dual-valued boolean fields" do
- @b.input(:platinum).to_s.should == '<label><input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
- @c.input(:platinum).to_s.should == '<label><input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
+ @b.input(:platinum).to_s.should == '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
+ @c.input(:platinum).to_s.should == '<input id="album_platinum_hidden" name="album[platinum]" type="hidden" value="f"/><label><input checked="checked" id="album_platinum" name="album[platinum]" type="checkbox" value="t"/> Platinum</label>'
end
specify "should use radio buttons for boolean fields if :as=>:radio is used" do

0 comments on commit d8bb95c

Please sign in to comment.