From ffc61dba7de8d7183b700123cdcf01ed819bba00 Mon Sep 17 00:00:00 2001 From: Jason Langenauer Date: Fri, 23 Apr 2010 22:56:15 +1000 Subject: [PATCH] Added specific not-an-integer error message to validates_numericality_of [#4406 state:resolved] --- activerecord/lib/active_record/locale/en.yml | 1 + activerecord/lib/active_record/validations.rb | 7 ++++++- activerecord/test/cases/validations_i18n_test.rb | 4 ++++ activerecord/test/cases/validations_test.rb | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index 2813524..4050b39 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -16,6 +16,7 @@ en: wrong_length: "is the wrong length (should be {{count}} characters)" taken: "has already been taken" not_a_number: "is not a number" + not_an_integer: "must be an integer" greater_than: "must be greater than {{count}}" greater_than_or_equal_to: "must be greater than or equal to {{count}}" equal_to: "must be equal to {{count}}" diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 568e530..0816db3 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -1021,10 +1021,15 @@ module ActiveRecord next if configuration[:allow_nil] and raw_value.nil? if configuration[:only_integer] + unless raw_value.to_s.index('.').nil? + record.errors.add(attr_name, :not_an_integer, :value => raw_value, :default => configuration[:message]) + next + end + unless raw_value.to_s =~ /\A[+-]?\d+\Z/ record.errors.add(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message]) next - end + end raw_value = raw_value.to_i else begin diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index f2e3e5a..03851db 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -757,6 +757,10 @@ class ActiveRecordDefaultErrorMessagesI18nTests < ActiveSupport::TestCase assert_default_error_message "is not a number", :not_a_number, :value => 'title' end + test "default error message: not_an_integer" do + assert_default_error_message "must be an integer", :not_an_integer, :value => 'title' + end + # used by: validates_numericality_of test "default error message: greater_than" do assert_default_error_message "must be greater than 10", :greater_than, :value => 'title', :count => 10 diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 00d915d..c68ac64 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -1615,6 +1615,18 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase assert_equal "greater than 4", topic.errors.on(:approved) end + def test_validates_numericality_with_correct_integer_only_message + Topic.validates_numericality_of :approved, :only_integer => true + + topic = Topic.new("title" => "numeric test", "approved" => 1.0) + assert !topic.valid? + assert_equal "must be an integer", topic.errors.on(:approved) + + topic = Topic.new("title" => "numeric test", "approved" => nil) + assert !topic.valid? + assert_equal "is not a number", topic.errors.on(:approved) + end + private def invalid!(values, error=nil) with_each_topic_approved_value(values) do |topic, value| -- 1.6.0.4