Skip to content
This repository
Browse code

Use .add instead of << to add errors

  • Loading branch information...
commit 78bab03804c05cf36554bce1835545eac14f2eb3 1 parent 077dffa
Evgeniy Dolzhenko authored October 11, 2011 vijaydev committed October 14, 2011
4  activemodel/lib/active_model/validations/validates.rb
@@ -27,7 +27,7 @@ module ClassMethods
27 27
       #
28 28
       #   class EmailValidator < ActiveModel::EachValidator
29 29
       #     def validate_each(record, attribute, value)
30  
-      #       record.errors[attribute] << (options[:message] || "is not an email") unless
  30
+      #       record.errors.add attribute, (options[:message] || "is not an email") unless
31 31
       #         value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
32 32
       #     end
33 33
       #   end
@@ -48,7 +48,7 @@ module ClassMethods
48 48
       #
49 49
       #     class TitleValidator < ActiveModel::EachValidator
50 50
       #       def validate_each(record, attribute, value)
51  
-      #         record.errors[attribute] << "must start with 'the'" unless value =~ /\Athe/i
  51
+      #         record.errors.add attribute, "must start with 'the'" unless value =~ /\Athe/i
52 52
       #       end
53 53
       #     end
54 54
       #
4  activemodel/lib/active_model/validations/with.rb
@@ -32,7 +32,7 @@ module ClassMethods
32 32
       #   class MyValidator < ActiveModel::Validator
33 33
       #     def validate(record)
34 34
       #       if some_complex_logic
35  
-      #         record.errors[:base] << "This record is invalid"
  35
+      #         record.errors.add :base, "This record is invalid"
36 36
       #       end
37 37
       #     end
38 38
       #
@@ -140,4 +140,4 @@ def validates_with(*args, &block)
140 140
       end
141 141
     end
142 142
   end
143  
-end
  143
+end
6  activemodel/lib/active_model/validator.rb
@@ -48,8 +48,8 @@ module ActiveModel #:nodoc:
48 48
   #
49 49
   #   class MyValidator < ActiveModel::Validator
50 50
   #     def validate(record)
51  
-  #       record.errors[:base] << "This is some custom error message"
52  
-  #       record.errors[:first_name] << "This is some complex validation"
  51
+  #       record.errors.add :base, "This is some custom error message"
  52
+  #       record.errors.add :first_name, "This is some complex validation"
53 53
   #       # etc...
54 54
   #     end
55 55
   #   end
@@ -68,7 +68,7 @@ module ActiveModel #:nodoc:
68 68
   #
69 69
   #   class TitleValidator < ActiveModel::EachValidator
70 70
   #     def validate_each(record, attribute, value)
71  
-  #       record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless value.in?(['Mr.', 'Mrs.', 'Dr.'])
  71
+  #       record.errors.add attribute, 'must be Mr. Mrs. or Dr.' unless value.in?(['Mr.', 'Mrs.', 'Dr.'])
72 72
   #     end
73 73
   #   end
74 74
   #

0 notes on commit 78bab03

Please sign in to comment.
Something went wrong with that request. Please try again.