Permalink
Browse files

Add :message option to validates_numericality_of. Closes #11456 [milo…

…ops, mdempfle]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9158 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
jeremy committed Mar 31, 2008
1 parent 6b9448c commit 3aaf60def848785a9c9c05c426359fa69888ef13
Showing with 17 additions and 1 deletion.
  1. +3 −1 activerecord/lib/active_record/validations.rb
  2. +14 −0 activerecord/test/cases/validations_test.rb
@@ -860,7 +860,9 @@ def validates_numericality_of(*attr_names)
when :odd, :even
record.errors.add(attr_name, configuration[:message] || ActiveRecord::Errors.default_error_messages[option]) unless raw_value.to_i.method(ALL_NUMERICALITY_CHECKS[option])[]
else
- record.errors.add(attr_name, configuration[:message] || (ActiveRecord::Errors.default_error_messages[option] % configuration[option])) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
+ message = configuration[:message] || ActiveRecord::Errors.default_error_messages[option]
+ message = message % configuration[option] if configuration[option]
+ record.errors.add(attr_name, message) unless raw_value.method(ALL_NUMERICALITY_CHECKS[option])[configuration[option]]
end
end
end
@@ -1405,6 +1405,20 @@ def test_validates_numericality_with_greater_than_less_than_and_even
valid!([2])
end
+ def test_validates_numericality_with_numeric_message
+ Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than %d"
+ topic = Topic.new("title" => "numeric test", "approved" => 10)
+
+ assert !topic.valid?
+ assert_equal "smaller than 4", topic.errors.on(:approved)
+
+ Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than %d"
+ topic = Topic.new("title" => "numeric test", "approved" => 1)
+
+ assert !topic.valid?
+ assert_equal "greater than 4", topic.errors.on(:approved)
+ end
+
private
def invalid!(values, error=nil)
with_each_topic_approved_value(values) do |topic, value|

0 comments on commit 3aaf60d

Please sign in to comment.