diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-03-31 01:49:31 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-03-31 01:49:31 +0000 |
commit | 3aaf60def848785a9c9c05c426359fa69888ef13 (patch) | |
tree | 9e800bd8f557daf8872a7440d68e17b9569328c7 | |
parent | 6b9448cdd227ef3adbe2f31ecaf64bc7ef062103 (diff) | |
download | rails-3aaf60def848785a9c9c05c426359fa69888ef13.tar.gz rails-3aaf60def848785a9c9c05c426359fa69888ef13.tar.bz2 rails-3aaf60def848785a9c9c05c426359fa69888ef13.zip |
Add :message option to validates_numericality_of. Closes #11456 [miloops, mdempfle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9158 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rwxr-xr-x | activerecord/lib/active_record/validations.rb | 4 | ||||
-rwxr-xr-x | activerecord/test/cases/validations_test.rb | 14 |
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index c321464335..aded3c0e85 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -860,7 +860,9 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 3d83b8d4d1..d5b8b4d88b 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -1405,6 +1405,20 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase 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| |