Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

ActiveRecord::Errors

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.

Public Class Methods

default_error_messages() click to toggle source
# File lib/active_record/validations.rb, line 135
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

Public Instance Methods

[](attribute) click to toggle source
Alias for: on
add(attribute, message = nil, options = {}) click to toggle source

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 lib/active_record/validations.rb, line 160
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
add_on_blank(attributes, custom_message = nil) click to toggle source

Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).

# File lib/active_record/validations.rb, line 178
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
add_on_empty(attributes, custom_message = nil) click to toggle source

Will add an error message to each of the attributes in attributes that is empty.

# File lib/active_record/validations.rb, line 169
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
add_to_base(msg) click to toggle source

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 lib/active_record/validations.rb, line 150
def add_to_base(msg)
  add(:base, msg)
end
clear() click to toggle source

Removes all errors that have been added.

# File lib/active_record/validations.rb, line 297
def clear
  @errors = ActiveSupport::OrderedHash.new
end
count() click to toggle source
Alias for: size
each() click to toggle source

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 lib/active_record/validations.rb, line 238
def each
  @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error.message } }
end
each_error() click to toggle source

Yields each attribute and associated error 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_error{|attr,err| puts "#{attr} - #{err.type}" }
# => name - :too_short
#    name - :blank
#    address - :blank
# File lib/active_record/validations.rb, line 254
def each_error
  @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } }
end
each_full() click to toggle source

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 lib/active_record/validations.rb, line 271
def each_full
  full_messages.each { |msg| yield msg }
end
empty?() click to toggle source

Returns true if no errors have been added.

# File lib/active_record/validations.rb, line 292
def empty?
  @errors.empty?
end
full_messages(options = {}) click to toggle source

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 lib/active_record/validations.rb, line 285
def full_messages(options = {})
  @errors.values.inject([]) do |full_messages, errors|
    full_messages + errors.map { |error| error.full_message }
  end
end
generate_message(attribute, message = :invalid, options = {}) click to toggle source
# File lib/active_record/validations.rb, line 335
def generate_message(attribute, message = :invalid, options = {})
  Error.new(@base, attribute, message, options).to_s
end
invalid?(attribute) click to toggle source

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 lib/active_record/validations.rb, line 195
def invalid?(attribute)
  !@errors[attribute.to_s].nil?
end
length() click to toggle source
Alias for: size
on(attribute) click to toggle source

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 lib/active_record/validations.rb, line 212
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
Also aliased as: []
on_base() click to toggle source

Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).

# File lib/active_record/validations.rb, line 222
def on_base
  on(:base)
end
size() click to toggle source

Returns the total number of errors added. Two errors added to the same attribute will be counted as such.

# File lib/active_record/validations.rb, line 302
def size
  @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size }
end
Also aliased as: count, length
to_xml(options={}) click to toggle source

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 lib/active_record/validations.rb, line 324
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.