Permalink
Browse files

Always run validator for changed conditionals

With the option `:if => :value_changed?` the validator will always run

With the option `:unless => :value_changed?` the validator will never
run

This is done because it is always assumed the value will change on the
form
  • Loading branch information...
1 parent 9ddfa56 commit ab43915b0a69e397288ffaa6d35b8e7217b4f259 @bcardarella bcardarella committed Nov 25, 2012
Showing with 4 additions and 14 deletions.
  1. +4 −6 lib/client_side_validations/active_model.rb
  2. +0 −8 test/active_model/cases/test_validations.rb
@@ -70,7 +70,9 @@ def can_use_for_client_side_validation?(attr, validator, force)
result = result && validator.kind != :block
if validator.options[:if] || validator.options[:unless]
- if result = can_force_validator?(attr, validator, force)
+ if validator.options[:if] && validator.options[:if] =~ /changed\?/
+ result = true
+ else result = can_force_validator?(attr, validator, force)
if validator.options[:if]
result = result && run_conditional(validator.options[:if])
end
@@ -125,11 +127,7 @@ def can_force_validator?(attr, validator, force)
false
end
else
- if (validator.options[:if] || validator.options[:unless]) =~ /changed\?/
- true
- else
- false
- end
+ false
end
end
end
@@ -365,20 +365,12 @@ def test_conditionals_forced_when_used_changed_helpers
p.validates :last_name, :presence => { :unless => :last_name_changed? }
end
- person.stubs(:first_name_changed?).returns(true)
- person.stubs(:last_name_changed?).returns(false)
-
expected_hash = {
:first_name => {
:presence => [{
:message => "can't be blank"
}]
},
- :last_name => {
- :presence => [{
- :message => "can't be blank"
- }]
- }
}
assert_equal expected_hash, person.client_side_validation_hash
end

0 comments on commit ab43915

Please sign in to comment.