Skip to content

Disable html5_validators in using :on #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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