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.
- []
- add
- add_on_blank
- add_on_boundary_breaking
- add_on_boundry_breaking
- add_on_empty
- add_to_base
- clear
- count
- each
- each_full
- empty?
- full_messages
- invalid?
- length
- on
- on_base
- size
- to_xml
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.
[ show 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?).
[ show 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.
[ show 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
Alias for add_on_boundary_breaking
[ show 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.
[ show 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.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 148 148: def clear 149: @errors = {} 150: end
Alias for size
Yields each attribute and associated message per error added.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 114 114: def each 115: @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } 116: 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".
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 120 120: def each_full 121: full_messages.each { |msg| yield msg } 122: end
Returns true if no errors have been added.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 143 143: def empty? 144: @errors.empty? 145: end
Returns all the full error messages in an array.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 125 125: def full_messages 126: full_messages = [] 127: 128: @errors.each_key do |attr| 129: @errors[attr].each do |msg| 130: next if msg.nil? 131: 132: if attr == "base" 133: full_messages << msg 134: else 135: full_messages << @base.class.human_attribute_name(attr) + " " + msg 136: end 137: end 138: end 139: full_messages 140: end
Returns true if the specified attribute has errors associated with it.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 93 93: def invalid?(attribute) 94: !@errors[attribute.to_s].nil? 95: end
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.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 100 100: def on(attribute) 101: errors = @errors[attribute.to_s] 102: return nil if errors.nil? 103: errors.size == 1 ? errors.first : errors 104: end
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 109 109: def on_base 110: on(:base) 111: end
Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 154 154: def size 155: @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } 156: end
Return an XML representation of this error object.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 162 162: def to_xml(options={}) 163: options[:root] ||= "errors" 164: options[:indent] ||= 2 165: options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 166: 167: options[:builder].instruct! unless options.delete(:skip_instruct) 168: options[:builder].errors do |e| 169: full_messages.each { |msg| e.error(msg) } 170: end 171: end