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.
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.
# 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?).
# 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.
# 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
Alias for add_on_boundary_breaking
Will add an error message to each of the attributes in attributes that is empty.
# 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.
# 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.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 152 152: def clear 153: @errors = {} 154: 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/rails/activerecord/lib/active_record/validations.rb, line 123 123: def each_full 124: full_messages.each { |msg| yield msg } 125: end
Returns true if no errors have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 147 147: def empty? 148: return @errors.empty? 149: end
Returns all the full error messages in an array.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 128 128: def full_messages 129: full_messages = [] 130: 131: @errors.each_key do |attr| 132: @errors[attr].each do |msg| 133: next if msg.nil? 134: 135: if attr == "base" 136: full_messages << msg 137: else 138: full_messages << @base.class.human_attribute_name(attr) + " " + msg 139: end 140: end 141: end 142: 143: return full_messages 144: end
Returns true if the specified attribute has errors associated with it.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 92 92: def invalid?(attribute) 93: !@errors[attribute.to_s].nil? 94: end
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 99 99: def on(attribute) 100: if @errors[attribute.to_s].nil? 101: nil 102: elsif @errors[attribute.to_s].length == 1 103: @errors[attribute.to_s].first 104: else 105: @errors[attribute.to_s] 106: end 107: end
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 112 112: def on_base 113: on(:base) 114: 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/rails/activerecord/lib/active_record/validations.rb, line 158 158: def size 159: error_count = 0 160: @errors.each_value { |attribute| error_count += attribute.length } 161: error_count 162: end