Class: ActiveRecord::Errors
- Inherits:
-
Object
- Object
- ActiveRecord::Errors
- Includes:
- Enumerable
- Defined in:
- lib/active_record/validations.rb
Overview
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.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(error_or_attr, message = nil, options = {}) ⇒ Object
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?. -
#add_on_blank(attributes, custom_message = nil) ⇒ Object
Will add an error message to each of the attributes in
attributesthat is blank (using Object#blank?). -
#add_on_empty(attributes, custom_message = nil) ⇒ Object
Will add an error message to each of the attributes in
attributesthat is empty. -
#add_to_base(msg) ⇒ Object
Adds an error to the base object instead of any particular attribute.
-
#clear ⇒ Object
Removes all errors that have been added.
-
#each ⇒ Object
Yields each attribute and associated message per error added.
- #each_error ⇒ Object
-
#each_full ⇒ Object
Yields each full error message added.
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#full_messages(options = {}) ⇒ Object
Returns all the full error messages in an array.
- #generate_message(attribute, message = :invalid, options = {}) ⇒ Object
-
#initialize(base) ⇒ Errors
constructor
:nodoc:.
-
#invalid?(attribute) ⇒ Boolean
Returns true if the specified
attributehas errors associated with it. -
#on(attribute) ⇒ Object
(also: #[])
Returns
nil, if no errors are associated with the specifiedattribute. -
#on_base ⇒ Object
Returns errors assigned to the base object through
add_to_baseaccording to the normal rules of on(attribute). -
#size ⇒ Object
(also: #count, #length)
Returns the total number of errors added.
-
#to_xml(options = {}) ⇒ Object
Returns an XML representation of this error object.
Constructor Details
#initialize(base) ⇒ Errors
:nodoc:
136 137 138 |
# File 'lib/active_record/validations.rb', line 136 def initialize(base) # :nodoc: @base, @errors = base, {} end |
Class Method Details
.default_error_messages ⇒ Object
130 131 132 133 |
# File 'lib/active_record/validations.rb', line 130 def ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').") I18n.translate 'activerecord.errors.messages' end |
Instance Method Details
#add(error_or_attr, message = nil, options = {}) ⇒ Object
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).
def add(attribute, message = nil, options = {})
message ||= :invalid
message = generate_message(attribute, message, options)) if message.is_a?(Symbol)
@errors ||= []
@errors << message
end
160 161 162 163 164 165 166 |
# File 'lib/active_record/validations.rb', line 160 def add(error_or_attr, = nil, = {}) error, attribute = error_or_attr.is_a?(Error) ? [error_or_attr, error_or_attr.attribute] : [nil, error_or_attr] [:message] = .delete(:default) if .has_key?(:default) @errors[attribute.to_s] ||= [] @errors[attribute.to_s] << (error || Error.new(@base, attribute, , )) end |
#add_on_blank(attributes, custom_message = nil) ⇒ Object
Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).
178 179 180 181 182 183 |
# File 'lib/active_record/validations.rb', line 178 def add_on_blank(attributes, = 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 => ) if value.blank? end end |
#add_on_empty(attributes, custom_message = nil) ⇒ Object
Will add an error message to each of the attributes in attributes that is empty.
169 170 171 172 173 174 175 |
# File 'lib/active_record/validations.rb', line 169 def add_on_empty(attributes, = 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 => ) unless !value.nil? && !is_empty end end |
#add_to_base(msg) ⇒ Object
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.
144 145 146 |
# File 'lib/active_record/validations.rb', line 144 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
Removes all errors that have been added.
285 286 287 |
# File 'lib/active_record/validations.rb', line 285 def clear @errors = {} end |
#each ⇒ Object
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
238 239 240 |
# File 'lib/active_record/validations.rb', line 238 def each @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error. } } end |
#each_error ⇒ Object
242 243 244 |
# File 'lib/active_record/validations.rb', line 242 def each_error @errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } } end |
#each_full ⇒ Object
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
259 260 261 |
# File 'lib/active_record/validations.rb', line 259 def each_full .each { |msg| yield msg } end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
280 281 282 |
# File 'lib/active_record/validations.rb', line 280 def empty? @errors.empty? end |
#full_messages(options = {}) ⇒ Object
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. # =>
["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
273 274 275 276 277 |
# File 'lib/active_record/validations.rb', line 273 def ( = {}) @errors.values.inject([]) do |, errors| + errors.map { |error| error. } end end |
#generate_message(attribute, message = :invalid, options = {}) ⇒ Object
323 324 325 326 |
# File 'lib/active_record/validations.rb', line 323 def (attribute, = :invalid, = {}) ActiveSupport::Deprecation.warn("ActiveRecord::Errors#generate_message has been deprecated. Please use ActiveRecord::Error#generate_message.") Error.new(@base, attribute, , ).to_s end |
#invalid?(attribute) ⇒ Boolean
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
195 196 197 |
# File 'lib/active_record/validations.rb', line 195 def invalid?(attribute) !@errors[attribute.to_s].nil? end |
#on(attribute) ⇒ Object Also known as: []
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
212 213 214 215 216 217 |
# 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 |
#on_base ⇒ Object
Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).
222 223 224 |
# File 'lib/active_record/validations.rb', line 222 def on_base on(:base) end |
#size ⇒ Object Also known as: count, length
Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
290 291 292 |
# File 'lib/active_record/validations.rb', line 290 def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end |
#to_xml(options = {}) ⇒ Object
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>
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/active_record/validations.rb', line 312 def to_xml(={}) [:root] ||= "errors" [:indent] ||= 2 [:builder] ||= Builder::XmlMarkup.new(:indent => [:indent]) [:builder].instruct! unless .delete(:skip_instruct) [:builder].errors do |e| .each { |msg| e.error(msg) } end end |