1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ensure validation errors to be ordered in declared order

[#2301 state:committed milestone:2.3.5]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Akira Matsuda 2009-03-19 14:42:08 +09:00 committed by Jeremy Kemper
parent 68b2b730e4
commit 0990a13500
2 changed files with 18 additions and 1 deletions

View file

@ -1,7 +1,8 @@
require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/string/inflections'
require 'active_support/ordered_hash'
module ActiveModel module ActiveModel
class Errors < Hash class Errors < ActiveSupport::OrderedHash
include DeprecatedErrorMethods include DeprecatedErrorMethods
def initialize(base) def initialize(base)

View file

@ -141,6 +141,22 @@ class ValidationsTest < ActiveModel::TestCase
t = Topic.new("title" => "") t = Topic.new("title" => "")
assert !t.valid? assert !t.valid?
assert_equal "can't be blank", t.errors["title"].first assert_equal "can't be blank", t.errors["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 => ''
assert !t.valid?
assert_equal :title, key = t.errors.keys.first
assert_equal "can't be blank", t.errors[key].first
assert_equal 'is too short (minimum is 2 characters)', t.errors[key].second
assert_equal :author_name, key = t.errors.keys.second
assert_equal "can't be blank", t.errors[key].first
assert_equal :author_email_address, key = t.errors.keys.third
assert_equal 'will never be valid', t.errors[key].first
assert_equal :content, key = t.errors.keys.fourth
assert_equal 'is too short (minimum is 2 characters)', t.errors[key].first
end end
def test_invalid_should_be_the_opposite_of_valid def test_invalid_should_be_the_opposite_of_valid