Class ActiveRecord::Errors
In: vendor/rails/activerecord/lib/active_record/validations.rb
Parent: Object

Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.

Methods

Included Modules

Enumerable

Public Instance methods

[](attribute)

Alias for on

Adds an error message (msg) to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute in which case an array will be returned on a call to on(attribute). If no msg is supplied, "invalid" is assumed.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 57
57:     def add(attribute, msg = @@default_error_messages[:invalid])
58:       @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
59:       @errors[attribute.to_s] << msg
60:     end

Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 72
72:     def add_on_blank(attributes, msg = @@default_error_messages[:blank])
73:       for attr in [attributes].flatten
74:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
75:         add(attr, msg) if value.blank?
76:       end
77:     end

Will add an error message to each of the attributes in attributes that has a length outside of the passed boundary range. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 81
81:     def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
82:       for attr in [attributes].flatten
83:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
84:         add(attr, too_short_msg % range.begin) if value && value.length < range.begin
85:         add(attr, too_long_msg % range.end) if value && value.length > range.end
86:       end
87:     end
add_on_boundry_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])

Will add an error message to each of the attributes in attributes that is empty.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 63
63:     def add_on_empty(attributes, msg = @@default_error_messages[:empty])
64:       for attr in [attributes].flatten
65:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
66:         is_empty = value.respond_to?("empty?") ? value.empty? : false
67:         add(attr, msg) unless !value.nil? && !is_empty
68:       end
69:     end

Adds an error to the base object instead of any particular attribute. This is used to report errors that don’t tie to any specific attribute, but rather to the object as a whole. These error messages don’t get prepended with any field name when iterating with each_full, so they should be complete sentences.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 49
49:     def add_to_base(msg)
50:       add(:base, msg)
51:     end

Removes all the errors that have been added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 147
147:     def clear
148:       @errors = {}
149:     end
count()

Alias for size

Yields each attribute and associated message per error added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 113
113:     def each
114:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
115:     end

Yields each full error message added. So Person.errors.add("first_name", "can’t be empty") will be returned through iteration as "First name can’t be empty".

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 119
119:     def each_full
120:       full_messages.each { |msg| yield msg }
121:     end

Returns true if no errors have been added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 142
142:     def empty?
143:       @errors.empty?
144:     end

Returns all the full error messages in an array.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 124
124:     def full_messages
125:       full_messages = []
126: 
127:       @errors.each_key do |attr|
128:         @errors[attr].each do |msg|
129:           next if msg.nil?
130: 
131:           if attr == "base"
132:             full_messages << msg
133:           else
134:             full_messages << @base.class.human_attribute_name(attr) + " " + msg
135:           end
136:         end
137:       end
138:       full_messages
139:     end

Returns true if the specified attribute has errors associated with it.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 92
92:     def invalid?(attribute)
93:       !@errors[attribute.to_s].nil?
94:     end
length()

Alias for size

  • Returns nil, if no errors are associated with the specified attribute.
  • Returns the error message, if one error is associated with the specified attribute.
  • Returns an array of error messages, if more than one error is associated with the specified attribute.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 99
 99:     def on(attribute)
100:       errors = @errors[attribute.to_s]
101:       return nil if errors.nil?
102:       errors.size == 1 ? errors.first : errors
103:     end

Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 108
108:     def on_base
109:       on(:base)
110:     end

Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 153
153:     def size
154:       @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
155:     end

Return an XML representation of this error object.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 161
161:     def to_xml(options={})
162:       options[:root] ||= "errors"
163:       options[:indent] ||= 2
164:       options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
165: 
166:       options[:builder].instruct! unless options.delete(:skip_instruct)
167:       options[:builder].errors do |e|
168:         full_messages.each { |msg| e.error(msg) }
169:       end
170:     end

[Validate]