Permalink
Browse files

Ensure validation errors to be ordered in declared order

[#2301 state:committed milestone:2.3.5]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
1 parent 596406f commit 44fbc86ab8975d9e254c7793d54996d2a9ce3f94 @amatsuda amatsuda committed with jeremy Mar 19, 2009
Showing with 19 additions and 8 deletions.
  1. +3 −2 activerecord/lib/active_record/validations.rb
  2. +16 −6 activerecord/test/cases/validations_test.rb
@@ -137,7 +137,8 @@ def default_error_messages
end
def initialize(base) # :nodoc:
- @base, @errors = base, {}
+ @base = base
+ clear
end
# Adds an error to the base object instead of any particular attribute. This is used
@@ -286,7 +287,7 @@ def empty?
# Removes all errors that have been added.
def clear
- @errors = {}
+ @errors = ActiveSupport::OrderedHash.new
end
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
@@ -1436,12 +1436,22 @@ def test_errors_to_xml
end
def test_validation_order
- Topic.validates_presence_of :title
- Topic.validates_length_of :title, :minimum => 2
-
- t = Topic.new("title" => "")
- assert !t.valid?
- assert_equal "can't be blank", t.errors.on("title").first
+ Topic.validates_presence_of :title, :author_name
+ Topic.validate {|topic| topic.errors.add('author_email_address', 'will never be valid')}
+ Topic.validates_length_of :title, :content, :minimum => 2
+
+ t = Topic.new :title => ''
+ t.valid?
+ e = t.errors.instance_variable_get '@errors'
+ assert_equal 'title', key = e.keys.first
+ assert_equal "can't be blank", t.errors.on(key).first
+ assert_equal 'is too short (minimum is 2 characters)', t.errors.on(key).second
+ assert_equal 'author_name', key = e.keys.second
+ assert_equal "can't be blank", t.errors.on(key)
+ assert_equal 'author_email_address', key = e.keys.third
+ assert_equal 'will never be valid', t.errors.on(key)
+ assert_equal 'content', key = e.keys.fourth
+ assert_equal 'is too short (minimum is 2 characters)', t.errors.on(key)
end
def test_invalid_should_be_the_opposite_of_valid

0 comments on commit 44fbc86

Please sign in to comment.