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.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 23 23: def default_error_messages 24: ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').") 25: I18n.translate 'activerecord.errors.messages' 26: end
Adds an error message (messsage) 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 messsage is supplied, :invalid is assumed. If message is a Symbol, it will be translated, using the appropriate scope (see translate_error).
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 46 46: def add(attribute, message = nil, options = {}) 47: message ||= :invalid 48: message = generate_message(attribute, message, options) if message.is_a?(Symbol) 49: @errors[attribute.to_s] ||= [] 50: @errors[attribute.to_s] << message 51: 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 63 63: def add_on_blank(attributes, custom_message = nil) 64: for attr in [attributes].flatten 65: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 66: add(attr, :blank, :default => custom_message) if value.blank? 67: end 68: 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 54 54: def add_on_empty(attributes, custom_message = nil) 55: for attr in [attributes].flatten 56: value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] 57: is_empty = value.respond_to?(:empty?) ? value.empty? : false 58: add(attr, :empty, :default => custom_message) unless !value.nil? && !is_empty 59: end 60: 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 37 37: def add_to_base(msg) 38: add(:base, msg) 39: end
Removes all errors that have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 221 221: def clear 222: @errors = {} 223: 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 165 165: def each 166: @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } 167: 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 182 182: def each_full 183: full_messages.each { |msg| yield msg } 184: end
Returns true if no errors have been added.
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 216 216: def empty? 217: @errors.empty? 218: 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 196 196: def full_messages(options = {}) 197: full_messages = [] 198: 199: @errors.each_key do |attr| 200: @errors[attr].each do |message| 201: next unless message 202: 203: if attr == "base" 204: full_messages << message 205: else 206: #key = :"activerecord.att.#{@base.class.name.underscore.to_sym}.#{attr}" 207: attr_name = @base.class.human_attribute_name(attr) 208: full_messages << attr_name + ' ' + message 209: end 210: end 211: end 212: full_messages 213: end
Translates an error message in it‘s default scope (activerecord.errrors.messages). Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it‘s not there, it‘s looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the default message (e.g. activerecord.errors.messages.MESSAGE). The translated model name, translated attribute name and the value are available for interpolation.
When using inheritence in your models, it will check all the inherited models too, but only if the model itself hasn‘t been found. Say you have class Admin < User; end and you wanted the translation for the :blank error message for the title attribute, it looks for these translations:
<ol> <li>activerecord.errors.models.admin.attributes.title.blank</li> <li>activerecord.errors.models.admin.blank</li> <li>activerecord.errors.models.user.attributes.title.blank</li> <li>activerecord.errors.models.user.blank</li> <li>activerecord.errors.messages.blank</li> <li>any default you provided through the options hash (in the activerecord.errors scope)</li> </ol>
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 88 88: def generate_message(attribute, message = :invalid, options = {}) 89: 90: message, options[:default] = options[:default], message if options[:default].is_a?(Symbol) 91: 92: defaults = @base.class.self_and_descendents_from_active_record.map do |klass| 93: [ "models.#{klass.name.underscore}.attributes.#{attribute}.#{message}""models.#{klass.name.underscore}.attributes.#{attribute}.#{message}", 94: "models.#{klass.name.underscore}.#{message}""models.#{klass.name.underscore}.#{message}" ] 95: end 96: 97: defaults << options.delete(:default) 98: defaults = defaults.compact.flatten << "messages.#{message}""messages.#{message}" 99: 100: key = defaults.shift 101: value = @base.respond_to?(attribute) ? @base.send(attribute) : nil 102: 103: options = { :default => defaults, 104: :model => @base.class.human_name, 105: :attribute => @base.class.human_attribute_name(attribute.to_s), 106: :value => value, 107: :scope => [:activerecord, :errors] 108: }.merge(options) 109: 110: I18n.translate(key, options) 111: 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 123 123: def invalid?(attribute) 124: !@errors[attribute.to_s].nil? 125: 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 140 140: def on(attribute) 141: errors = @errors[attribute.to_s] 142: return nil if errors.nil? 143: errors.size == 1 ? errors.first : errors 144: 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 149 149: def on_base 150: on(:base) 151: 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 226 226: def size 227: @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } 228: end
Returns 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 248 248: def to_xml(options={}) 249: options[:root] ||= "errors" 250: options[:indent] ||= 2 251: options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) 252: 253: options[:builder].instruct! unless options.delete(:skip_instruct) 254: options[:builder].errors do |e| 255: full_messages.each { |msg| e.error(msg) } 256: end 257: end