diff --git a/lib/html5_validators/active_model/helper_methods.rb b/lib/html5_validators/active_model/helper_methods.rb index 3891772..381fa00 100644 --- a/lib/html5_validators/active_model/helper_methods.rb +++ b/lib/html5_validators/active_model/helper_methods.rb @@ -3,31 +3,31 @@ module Validations module HelperMethods def attribute_required?(attribute) self.validators.grep(PresenceValidator).any? do |v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:if, :unless, :on]).empty? end end def attribute_maxlength(attribute) self.validators.grep(LengthValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :tokenizer]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:maximum, :is]).any? && (v.options.keys & [:if, :unless, :on, :tokenizer]).empty? }.map {|v| v.options.slice(:maximum, :is)}.map(&:values).flatten.max end def attribute_minlength(attribute) self.validators.grep(LengthValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank, :tokenizer]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:minimum, :is]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank, :tokenizer]).empty? }.map {|v| v.options.slice(:minimum, :is)}.map(&:values).flatten.min end def attribute_max(attribute) self.validators.grep(NumericalityValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:less_than, :less_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank]).empty? }.map {|v| v.options.slice(:less_than, :less_than_or_equal_to)}.map(&:values).flatten.max end def attribute_min(attribute) self.validators.grep(NumericalityValidator).select {|v| - v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :allow_nil, :allow_blank]).empty? + v.attributes.include?(attribute.to_sym) && (v.options.keys & [:greater_than, :greater_than_or_equal_to]).any? && (v.options.keys & [:if, :unless, :on, :allow_nil, :allow_blank]).empty? }.map {|v| v.options.slice(:greater_than, :greater_than_or_equal_to)}.map(&:values).flatten.min end end diff --git a/spec/features/validation_spec.rb b/spec/features/validation_spec.rb index 9bed690..67b6491 100644 --- a/spec/features/validation_spec.rb +++ b/spec/features/validation_spec.rb @@ -100,6 +100,21 @@ find('textarea#person_bio')[:minlength].should == '10' end end + + context 'with validation context' do + background do + Person.validates_presence_of :name, :bio, {:on => :create} + end + after do + Person._validators.clear + end + scenario 'new form' do + visit '/people/new' + + find('input#person_name')[:required].should be_nil + find('textarea#person_bio')[:required].should be_nil + end + end end feature 'item#new' do @@ -202,4 +217,19 @@ find('textarea#item_description')[:minlength].should == '10' end end + + context 'with validation context' do + background do + Item.validates_presence_of :name, :description, {:on => :create} + end + after do + Item._validators.clear + end + scenario 'new form' do + visit '/items/new' + + find('input#item_name')[:required].should be_nil + find('textarea#item_description')[:required].should be_nil + end + end end