class ActiveRecord::Errors
determine whether the object is in a valid state to be saved. See usage example in Validations.
Active Record validation is reported to and from this object, which is used by Base#save to
def add(attribute, message = nil, options = {})
If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error).
If no +messsage+ is supplied, :invalid is assumed.
error can be added to the same +attribute+ in which case an array 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
Adds an error message (+messsage+) to the +attribute+, which will be returned on a call to on(attribute)
def add(attribute, message = nil, options = {}) options[:message] = options.delete(:default) if options[:default].is_a?(Symbol) error, message = message, nil if message.is_a?(Error) @errors[attribute.to_s] ||= [] @errors[attribute.to_s] << (error || Error.new(@base, attribute, message, options)) end
def add_on_blank(attributes, custom_message = nil)
def add_on_blank(attributes, custom_message = nil) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] add(attr, :blank, :default => custom_message) if value.blank? end end
def add_on_empty(attributes, custom_message = nil)
def add_on_empty(attributes, custom_message = nil) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] is_empty = value.respond_to?(:empty?) ? value.empty? : false add(attr, :empty, :default => custom_message) unless !value.nil? && !is_empty end end
def add_to_base(msg)
as a whole. These error messages don't get prepended with any field name when iterating
to report errors that don't tie to any specific attribute, but rather to the object
Adds an error to the base object instead of any particular attribute. This is used
def add_to_base(msg) add(:base, msg) end
def clear
def clear @errors = ActiveSupport::OrderedHash.new end
def default_error_messages
def default_error_messages ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').") I18n.translate 'activerecord.errors.messages' end
def each
# name - can't be blank
# => name - is too short (minimum is 5 characters)
company.errors.each{|attr,msg| puts "#{attr} - #{msg}" }
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Yields each attribute and associated message per error added.
def each @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error.message } } end
def each_error
# name - :blank
# => name - :too_short
company.errors.each_error{|attr,err| puts "#{attr} - #{err.type}" }
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Yields each attribute and associated error per error added.
def each_error @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } } end
def each_full
# Name can't be blank
# => Name is too short (minimum is 5 characters)
company.errors.each_full{|msg| puts msg }
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
through iteration as "First name can't be empty".
Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned
def each_full full_messages.each { |msg| yield msg } end
def empty?
def empty? @errors.empty? end
def full_messages(options = {})
company.errors.full_messages # =>
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Returns all the full error messages in an array.
def full_messages(options = {}) @errors.values.inject([]) do |full_messages, errors| full_messages + errors.map { |error| error.full_message } end end
def generate_message(attribute, message = :invalid, options = {})
def generate_message(attribute, message = :invalid, options = {}) ActiveSupport::Deprecation.warn("ActiveRecord::Errors#generate_message has been deprecated. Please use ActiveRecord::Error.new().to_s.") Error.new(@base, attribute, message, options).to_s end
def initialize(base) # :nodoc:
def initialize(base) # :nodoc: @base = base clear end
def invalid?(attribute)
company.errors.invalid?(:name) # => true
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Returns true if the specified +attribute+ has errors associated with it.
def invalid?(attribute) !@errors[attribute.to_s].nil? end
def on(attribute)
company.errors.on(:email) # => "can't be blank"
company.errors.on(:name) # => ["is too short (minimum is 5 characters)", "can't be blank"]
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Returns an array of error messages, if more than one error is associated with the specified +attribute+.
Returns the error message, if one error is associated with the specified +attribute+.
Returns +nil+, if no errors are associated with the specified +attribute+.
def on(attribute) attribute = attribute.to_s return nil unless @errors.has_key?(attribute) errors = @errors[attribute].map(&:to_s) errors.size == 1 ? errors.first : errors end
def on_base
def on_base on(:base) end
def size
def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end
def to_xml(options={})
#
#
#
#
# =>
company.errors.to_xml
company = Company.create(:address => '123 First St.')
end
validates_length_of :name, :in => 5..30
validates_presence_of :name, :address, :email
class Company < ActiveRecord::Base
Returns an XML representation of this error object.
def to_xml(options={}) options[:root] ||= "errors" options[:indent] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) options[:builder].instruct! unless options.delete(:skip_instruct) options[:builder].errors do |e| full_messages.each { |msg| e.error(msg) } end end