Class: ActiveRecord::Errors
Overview
Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object in a valid state to be saved. See usage example in Validations.
Constant Summary collapse
- @@default_error_messages =
{ :inclusion => "is not included in the list", :invalid => "is invalid", :confirmation => "doesn't match confirmation", :accepted => "must be accepted", :empty => "can't be empty", :too_long => "is too long (max is %d characters)", :too_short => "is too short (min is %d characters)", :wrong_length => "is the wrong length (should be %d characters)", :taken => "has already been taken", }
Instance Method Summary collapse
-
#add(attribute, msg = ) ⇒ Object
Adds an error message (
msg) to theattribute, which will be returned on a call toon(attribute)for the same attribute and ensure that this error object returns false when asked ifempty?. -
#add_on_boundary_breaking(attributes, range, too_long_msg = , too_short_msg = ) ⇒ Object
(also: #add_on_boundry_breaking)
Will add an error message to each of the attributes in
attributesthat has a length outside of the passed boundaryrange. -
#add_on_empty(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in
attributesthat is empty (defined byattribute_present?). -
#add_to_base(msg) ⇒ Object
Adds an error to the base object instead of any particular attribute.
-
#clear ⇒ Object
Removes all the errors that have been added.
-
#count ⇒ Object
Returns the total number of errors added.
-
#each ⇒ Object
Yields each attribute and associated message per error added.
-
#each_full ⇒ Object
Yields each full error message added.
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#full_messages ⇒ Object
Returns all the full error messages in an array.
-
#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 specified
attribute.
-
-
#on_base ⇒ Object
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
Constructor Details
#initialize(base) ⇒ Errors
:nodoc:
372 373 374 |
# File 'lib/active_record/validations.rb', line 372 def initialize(base) # :nodoc: @base, @errors = base, {} end |
Instance Method Details
#add(attribute, msg = ) ⇒ Object
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.
402 403 404 405 |
# File 'lib/active_record/validations.rb', line 402 def add(attribute, msg = [:invalid]) @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? @errors[attribute.to_s] << msg end |
#add_on_boundary_breaking(attributes, range, too_long_msg = , too_short_msg = ) ⇒ Object Also known as: add_on_boundry_breaking
Will add an error message to each of the attributes in attributes that has a length outside of the passed boundary range. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.
418 419 420 421 422 423 424 |
# File 'lib/active_record/validations.rb', line 418 def add_on_boundary_breaking(attributes, range, too_long_msg = [:too_long], too_short_msg = [:too_short]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] add(attr, too_short_msg % range.begin) if value && value.length < range.begin add(attr, too_long_msg % range.end) if value && value.length > range.end end end |
#add_on_empty(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in attributes that is empty (defined by attribute_present?).
408 409 410 411 412 413 414 |
# File 'lib/active_record/validations.rb', line 408 def add_on_empty(attributes, msg = [:empty]) 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, msg) 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 doesn’t tie to any specific attribute, but rather to the object as a whole. These error messages doesn’t get prepended with any field name when iterating with each_full, so they should be complete sentences.
394 395 396 |
# File 'lib/active_record/validations.rb', line 394 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
Removes all the errors that have been added.
489 490 491 |
# File 'lib/active_record/validations.rb', line 489 def clear @errors = {} end |
#count ⇒ Object
Returns the total number of errors added. Two errors added to the same attribute will be counted as such with this as well.
495 496 497 498 499 |
# File 'lib/active_record/validations.rb', line 495 def count error_count = 0 @errors.each_value { |attribute| error_count += attribute.length } error_count end |
#each ⇒ Object
Yields each attribute and associated message per error added.
454 455 456 |
# File 'lib/active_record/validations.rb', line 454 def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } 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”.
460 461 462 |
# File 'lib/active_record/validations.rb', line 460 def each_full .each { |msg| yield msg } end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
484 485 486 |
# File 'lib/active_record/validations.rb', line 484 def empty? return @errors.empty? end |
#full_messages ⇒ Object
Returns all the full error messages in an array.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/active_record/validations.rb', line 465 def = [] @errors.each_key do |attr| @errors[attr].each do |msg| next if msg.nil? if attr == "base" << msg else << @base.class.human_attribute_name(attr) + " " + msg end end end return end |
#invalid?(attribute) ⇒ Boolean
Returns true if the specified attribute has errors associated with it.
429 430 431 |
# File 'lib/active_record/validations.rb', line 429 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.
436 437 438 439 440 441 442 443 444 |
# File 'lib/active_record/validations.rb', line 436 def on(attribute) if @errors[attribute.to_s].nil? nil elsif @errors[attribute.to_s].length == 1 @errors[attribute.to_s].first else @errors[attribute.to_s] end end |
#on_base ⇒ Object
Returns errors assigned to base object through add_to_base according to the normal rules of on(attribute).
449 450 451 |
# File 'lib/active_record/validations.rb', line 449 def on_base on(:base) end |