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 is 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 64 64: def add(attribute, msg = @@default_error_messages[:invalid]) 65: @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? 66: @errors[attribute.to_s] << msg 67: 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 79 79: def add_on_blank(attributes, msg = @@default_error_messages[:blank]) 80: for attr in [attributes].flatten 81: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 82: add(attr, msg) if value.blank? 83: end 84: end
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 70 70: def add_on_empty(attributes, msg = @@default_error_messages[:empty]) 71: for attr in [attributes].flatten 72: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 73: is_empty = value.respond_to?("empty?") ? value.empty? : false 74: add(attr, msg) unless !value.nil? && !is_empty 75: end 76: 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 56 56: def add_to_base(msg) 57: add(:base, msg) 58: end
Removes all errors that have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 192 192: def clear 193: @errors = {} 194: end
Yields each attribute and associated message per error added.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.each{|attr,msg| puts "#{attr} - #{msg}" } # => name - is too short (minimum is 5 characters) name - can't be blank address - can't be blank
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 138 138: def each 139: @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } 140: 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".
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.each_full{|msg| puts msg } # => Name is too short (minimum is 5 characters) Name can't be blank Address can't be blank
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 155 155: def each_full 156: full_messages.each { |msg| yield msg } 157: end
Returns true if no errors have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 187 187: def empty? 188: @errors.empty? 189: end
Returns all the full error messages in an array.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.full_messages # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 169 169: def full_messages 170: full_messages = [] 171: 172: @errors.each_key do |attr| 173: @errors[attr].each do |msg| 174: next if msg.nil? 175: 176: if attr == "base" 177: full_messages << msg 178: else 179: full_messages << @base.class.human_attribute_name(attr) + " " + msg 180: end 181: end 182: end 183: full_messages 184: end
Returns true if the specified attribute has errors associated with it.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.invalid?(:name) # => true company.errors.invalid?(:address) # => false
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 96 96: def invalid?(attribute) 97: !@errors[attribute.to_s].nil? 98: end
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.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.on(:name) # => ["is too short (minimum is 5 characters)", "can't be blank"] company.errors.on(:email) # => "can't be blank" company.errors.on(:address) # => nil
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 113 113: def on(attribute) 114: errors = @errors[attribute.to_s] 115: return nil if errors.nil? 116: errors.size == 1 ? errors.first : errors 117: end
Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 122 122: def on_base 123: on(:base) 124: end
Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 197 197: def size 198: @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } 199: end
Return an XML representation of this error object.
class Company < ActiveRecord::Base validates_presence_of :name, :address, :email validates_length_of :name, :in => 5..30 end company = Company.create(:address => '123 First St.') company.errors.to_xml # => <?xml version="1.0" encoding="UTF-8"?> <errors> <error>Name is too short (minimum is 5 characters)</error> <error>Name can't be blank</error> <error>Address can't be blank</error> </errors>
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 219 219: def to_xml(options={}) 220: options[:root] ||= "errors" 221: options[:indent] ||= 2 222: options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 223: 224: options[:builder].instruct! unless options.delete(:skip_instruct) 225: options[:builder].errors do |e| 226: full_messages.each { |msg| e.error(msg) } 227: end 228: end