Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/html5_validators/active_model/helper_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/features/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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