| In: |
vendor/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.
Adds an error to the base object instead of any particular attribute. This is used to report errors that doesn’t tie to any specific attribute, but rather to the object as a whole. These error messages doesn’t get prepended with any field name when iterating with each_full, so they should be complete sentences.
# File vendor/activerecord/lib/active_record/validations.rb, line 105
105: def add_to_base(msg)
106: add(:base, msg)
107: end
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, true is assumed.
# File vendor/activerecord/lib/active_record/validations.rb, line 113
113: def add(attribute, msg = true)
114: @errors[attribute] = [] if @errors[attribute].nil?
115: @errors[attribute] << msg
116: end
Will add an error message to each of the attributes in attributes that is empty (defined by attribute_present?).
# File vendor/activerecord/lib/active_record/validations.rb, line 119
119: def add_on_empty(attributes, msg = "can't be empty")
120: if attributes.kind_of?(Array)
121: attributes.each { |attr| add(attr, msg) unless @base.attribute_present?(attr) }
122: else
123: add(attributes, msg) unless @base.attribute_present?(attributes)
124: end
125: end
Returns true if the specified attribute has errors associated with it.
# File vendor/activerecord/lib/active_record/validations.rb, line 128
128: def invalid?(attribute)
129: !@errors[attribute].nil?
130: end
# File vendor/activerecord/lib/active_record/validations.rb, line 135
135: def on(attribute)
136: if @errors[attribute].nil?
137: nil
138: elsif @errors[attribute].length == 1
139: @errors[attribute].first
140: else
141: @errors[attribute]
142: end
143: end
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
# File vendor/activerecord/lib/active_record/validations.rb, line 146
146: def on_base
147: on(:base)
148: end
Yields each attribute and associated message per error added.
# File vendor/activerecord/lib/active_record/validations.rb, line 151
151: def each
152: @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
153: 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".
# File vendor/activerecord/lib/active_record/validations.rb, line 157
157: def each_full
158: @errors.each_key do |attr|
159: @errors[attr].each do |msg|
160: if attr == :base
161: yield msg
162: else
163: yield @base.class.human_attribute_name(attr) + " " + msg
164: end
165: end
166: end
167: end
Returns true if no errors have been added.
# File vendor/activerecord/lib/active_record/validations.rb, line 170
170: def empty?
171: return @errors.empty?
172: end
Removes all the errors that have been added.
# File vendor/activerecord/lib/active_record/validations.rb, line 175
175: def clear
176: @errors = {}
177: end
Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.
# File vendor/activerecord/lib/active_record/validations.rb, line 181
181: def count
182: error_count = 0
183: @errors.each_value { |attribute| error_count += attribute.length }
184: error_count
185: end