2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:02 -04:00
|
|
|
require "active_support/core_ext/array/conversions"
|
|
|
|
require "active_support/core_ext/string/inflections"
|
|
|
|
require "active_support/core_ext/object/deep_dup"
|
|
|
|
require "active_support/core_ext/string/filters"
|
2018-03-20 10:39:44 -04:00
|
|
|
require "active_support/deprecation"
|
|
|
|
require "active_model/error"
|
|
|
|
require "active_model/nested_error"
|
|
|
|
require "forwardable"
|
2009-06-08 21:32:08 -04:00
|
|
|
|
2008-03-31 19:40:34 -04:00
|
|
|
module ActiveModel
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model \Errors
|
2010-06-14 05:10:57 -04:00
|
|
|
#
|
2012-02-07 13:53:03 -05:00
|
|
|
# Provides a modified +Hash+ that you can include in your object
|
2013-12-24 06:39:41 -05:00
|
|
|
# for handling error messages and interacting with Action View helpers.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
|
|
|
# # Required dependency for ActiveModel::Errors
|
|
|
|
# extend ActiveModel::Naming
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def initialize
|
|
|
|
# @errors = ActiveModel::Errors.new(self)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :name
|
|
|
|
# attr_reader :errors
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def validate!
|
2015-01-04 03:18:03 -05:00
|
|
|
# errors.add(:name, :blank, message: "cannot be nil") if name.nil?
|
2010-01-31 18:08:20 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# # The following methods are needed to be minimally implemented
|
|
|
|
#
|
|
|
|
# def read_attribute_for_validation(attr)
|
|
|
|
# send(attr)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-03-22 18:10:28 -04:00
|
|
|
# def self.human_attribute_name(attr, options = {})
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-03-22 18:10:28 -04:00
|
|
|
# def self.lookup_ancestors
|
2010-01-31 18:08:20 -05:00
|
|
|
# [self]
|
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-05-29 16:27:23 -04:00
|
|
|
# The last three methods are required in your object for +Errors+ to be
|
2010-01-31 18:08:20 -05:00
|
|
|
# able to generate error messages correctly and also handle multiple
|
2015-05-29 16:27:23 -04:00
|
|
|
# languages. Of course, if you extend your object with <tt>ActiveModel::Translation</tt>
|
2011-05-23 19:39:10 -04:00
|
|
|
# you will not need to implement the last two. Likewise, using
|
2015-05-29 16:27:23 -04:00
|
|
|
# <tt>ActiveModel::Validations</tt> will handle the validation related methods
|
2010-01-31 18:08:20 -05:00
|
|
|
# for you.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# The above allows you to do:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2013-06-29 12:17:50 -04:00
|
|
|
# person = Person.new
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.validate! # => ["cannot be nil"]
|
|
|
|
# person.errors.full_messages # => ["name cannot be nil"]
|
2010-01-31 18:08:20 -05:00
|
|
|
# # etc..
|
2011-02-08 20:40:49 -05:00
|
|
|
class Errors
|
|
|
|
include Enumerable
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
extend Forwardable
|
2018-04-03 01:06:04 -04:00
|
|
|
def_delegators :@errors, :size, :clear, :blank?, :empty?, :uniq!
|
2018-04-02 22:52:36 -04:00
|
|
|
# TODO: forward all enumerable methods after `each` deprecation is removed.
|
|
|
|
def_delegators :@errors, :count
|
2018-03-20 10:39:44 -04:00
|
|
|
|
|
|
|
LEGACY_ATTRIBUTES = [:messages, :details].freeze
|
|
|
|
|
2018-05-22 17:20:14 -04:00
|
|
|
class << self
|
2019-03-29 12:08:48 -04:00
|
|
|
attr_accessor :i18n_customize_full_message # :nodoc:
|
2018-05-22 17:20:14 -04:00
|
|
|
end
|
2019-03-29 12:08:48 -04:00
|
|
|
self.i18n_customize_full_message = false
|
2018-05-22 17:20:14 -04:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
attr_reader :errors
|
2018-04-02 12:17:45 -04:00
|
|
|
alias :objects :errors
|
2011-02-08 20:40:49 -05:00
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Pass in the instance of the object that is using the errors object.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
|
|
|
# def initialize
|
|
|
|
# @errors = ActiveModel::Errors.new(self)
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-19 19:28:59 -04:00
|
|
|
def initialize(base)
|
2018-03-20 10:39:44 -04:00
|
|
|
@base = base
|
|
|
|
@errors = []
|
2011-02-08 20:40:49 -05:00
|
|
|
end
|
|
|
|
|
2012-10-22 15:27:55 -04:00
|
|
|
def initialize_dup(other) # :nodoc:
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors = other.errors.deep_dup
|
2012-01-17 02:07:15 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-12-03 13:18:57 -05:00
|
|
|
# Copies the errors from <tt>other</tt>.
|
2018-03-20 10:39:44 -04:00
|
|
|
# For copying errors but keep <tt>@base</tt> as is.
|
2015-12-03 13:18:57 -05:00
|
|
|
#
|
|
|
|
# other - The ActiveModel::Errors instance.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# person.errors.copy!(other)
|
|
|
|
def copy!(other) # :nodoc:
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors = other.errors.deep_dup
|
|
|
|
@errors.each { |error|
|
|
|
|
error.instance_variable_set("@base", @base)
|
|
|
|
}
|
2015-12-03 13:18:57 -05:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
# Imports one error
|
|
|
|
# Imported errors are wrapped as a NestedError,
|
|
|
|
# providing access to original error object.
|
|
|
|
# If attribute or type needs to be overriden, use `override_options`.
|
|
|
|
#
|
|
|
|
# override_options - Hash
|
|
|
|
# @option override_options [Symbol] :attribute Override the attribute the error belongs to
|
|
|
|
# @option override_options [Symbol] :type Override type of the error.
|
|
|
|
def import(error, override_options = {})
|
2018-04-03 02:14:42 -04:00
|
|
|
[:attribute, :type].each do |key|
|
|
|
|
if override_options.key?(key)
|
|
|
|
override_options[key] = override_options[key].to_sym
|
|
|
|
end
|
|
|
|
end
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors.append(NestedError.new(@base, error, override_options))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Merges the errors from <tt>other</tt>,
|
|
|
|
# each <tt>Error</tt> wrapped as <tt>NestedError</tt>.
|
2017-07-07 13:16:06 -04:00
|
|
|
#
|
|
|
|
# other - The ActiveModel::Errors instance.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# person.errors.merge!(other)
|
|
|
|
def merge!(other)
|
2018-03-20 10:39:44 -04:00
|
|
|
other.errors.each { |error|
|
|
|
|
import(error)
|
|
|
|
}
|
2017-07-07 13:16:06 -04:00
|
|
|
end
|
|
|
|
|
2018-11-20 18:54:22 -05:00
|
|
|
# Removes all errors except the given keys. Returns a hash containing the removed errors.
|
|
|
|
#
|
|
|
|
# person.errors.keys # => [:name, :age, :gender, :city]
|
|
|
|
# person.errors.slice!(:age, :gender) # => { :name=>["cannot be nil"], :city=>["cannot be nil"] }
|
|
|
|
# person.errors.keys # => [:age, :gender]
|
|
|
|
def slice!(*keys)
|
|
|
|
keys = keys.map(&:to_sym)
|
2018-03-20 10:39:44 -04:00
|
|
|
|
|
|
|
results = messages.slice!(*keys)
|
|
|
|
|
|
|
|
@errors.keep_if do |error|
|
|
|
|
keys.include?(error.attribute)
|
|
|
|
end
|
|
|
|
|
|
|
|
results
|
2018-11-20 18:54:22 -05:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
# Search for errors matching +attribute+, +type+ or +options+.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2018-03-20 10:39:44 -04:00
|
|
|
# Only supplied params will be matched.
|
|
|
|
#
|
|
|
|
# person.errors.where(:name) # => all name errors.
|
|
|
|
# person.errors.where(:name, :too_short) # => all name errors being too short
|
|
|
|
# person.errors.where(:name, :too_short, minimum: 2) # => all name errors being too short and minimum is 2
|
|
|
|
def where(attribute, type = nil, **options)
|
|
|
|
attribute, type, options = normalize_arguments(attribute, type, options)
|
|
|
|
@errors.select { |error|
|
|
|
|
error.match?(attribute, type, options)
|
|
|
|
}
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns +true+ if the error messages include an error for the given key
|
2012-06-22 17:48:50 -04:00
|
|
|
# +attribute+, +false+ otherwise.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil"]}
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.include?(:name) # => true
|
2012-10-22 15:27:55 -04:00
|
|
|
# person.errors.include?(:age) # => false
|
2012-06-22 13:42:32 -04:00
|
|
|
def include?(attribute)
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors.any? { |error|
|
|
|
|
error.match?(attribute.to_sym)
|
|
|
|
}
|
2011-02-09 12:19:15 -05:00
|
|
|
end
|
2011-09-06 12:35:12 -04:00
|
|
|
alias :has_key? :include?
|
2014-10-14 04:53:26 -04:00
|
|
|
alias :key? :include?
|
2011-02-09 12:19:15 -05:00
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Delete messages for +key+. Returns the deleted messages.
|
|
|
|
#
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors[:name] # => ["cannot be nil"]
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.delete(:name) # => ["cannot be nil"]
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors[:name] # => []
|
2018-03-20 10:39:44 -04:00
|
|
|
def delete(attribute, type = nil, **options)
|
|
|
|
attribute, type, options = normalize_arguments(attribute, type, options)
|
|
|
|
matches = where(attribute, type, options)
|
|
|
|
matches.each do |error|
|
|
|
|
@errors.delete(error)
|
|
|
|
end
|
|
|
|
matches.map(&:message)
|
2012-01-16 11:22:44 -05:00
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# When passed a symbol or a name of a method, returns an array of errors
|
2010-06-14 05:10:57 -04:00
|
|
|
# for the method.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors[:name] # => ["cannot be nil"]
|
|
|
|
# person.errors['name'] # => ["cannot be nil"]
|
2008-03-31 19:40:34 -04:00
|
|
|
def [](attribute)
|
2018-04-02 22:52:36 -04:00
|
|
|
messages_for(attribute)
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Iterates through each error key, value pair in the error messages hash.
|
2011-05-23 19:39:10 -04:00
|
|
|
# Yields the attribute and the error for that attribute. If the attribute
|
2010-01-31 18:08:20 -05:00
|
|
|
# has more than one error message, yields once for each error message.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :blank, message: "can't be blank")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.each do |attribute, error|
|
2010-01-31 18:08:20 -05:00
|
|
|
# # Will yield :name and "can't be blank"
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :not_specified, message: "must be specified")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.each do |attribute, error|
|
2010-01-31 18:08:20 -05:00
|
|
|
# # Will yield :name and "can't be blank"
|
|
|
|
# # then yield :name and "must be specified"
|
|
|
|
# end
|
2018-03-20 10:39:44 -04:00
|
|
|
def each(&block)
|
|
|
|
if block.arity == 1
|
|
|
|
@errors.each(&block)
|
|
|
|
else
|
|
|
|
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
|
|
|
Enumerating ActiveModel::Errors as a hash has been deprecated.
|
|
|
|
In Rails 6, `errors` is an array of Error objects,
|
|
|
|
therefore it should be accessed by a block with a single block
|
|
|
|
parameter like this:
|
|
|
|
|
|
|
|
person.errors.each do |error|
|
|
|
|
error.full_message
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
You are passing a block expecting 2 parameters,
|
|
|
|
so the old hash behavior is simulated. As this is deprecated,
|
|
|
|
this will result in an ArgumentError in Rails 6.1.
|
|
|
|
MSG
|
|
|
|
@errors.
|
|
|
|
sort { |a, b| a.attribute <=> b.attribute }.
|
|
|
|
each { |error| yield error.attribute, error.message }
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns all message values.
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
|
|
|
# person.errors.values # => [["cannot be nil", "must be specified"]]
|
2011-02-08 20:40:49 -05:00
|
|
|
def values
|
2018-03-20 10:39:44 -04:00
|
|
|
deprecation_removal_warning(:values)
|
|
|
|
@errors.map(&:message).freeze
|
2011-02-08 20:40:49 -05:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns all message keys.
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.keys # => [:name]
|
2011-02-08 20:40:49 -05:00
|
|
|
def keys
|
2018-03-20 10:39:44 -04:00
|
|
|
deprecation_removal_warning(:keys)
|
|
|
|
keys = @errors.map(&:attribute)
|
|
|
|
keys.uniq!
|
|
|
|
keys.freeze
|
2010-04-09 10:48:24 -04:00
|
|
|
end
|
2011-09-09 04:28:25 -04:00
|
|
|
|
2010-01-31 18:08:20 -05:00
|
|
|
# Returns an xml formatted representation of the Errors hash.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :blank, message: "can't be blank")
|
|
|
|
# person.errors.add(:name, :not_specified, message: "must be specified")
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.to_xml
|
2010-07-29 20:30:04 -04:00
|
|
|
# # =>
|
2010-01-31 18:08:20 -05:00
|
|
|
# # <?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
|
# # <errors>
|
|
|
|
# # <error>name can't be blank</error>
|
|
|
|
# # <error>name must be specified</error>
|
|
|
|
# # </errors>
|
2016-10-28 23:05:58 -04:00
|
|
|
def to_xml(options = {})
|
2018-03-20 10:39:44 -04:00
|
|
|
deprecation_removal_warning(:to_xml)
|
2013-05-01 20:10:06 -04:00
|
|
|
to_a.to_xml({ root: "errors", skip_types: true }.merge!(options))
|
2010-06-26 05:57:43 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-06-22 17:04:16 -04:00
|
|
|
# Returns a Hash that can be used as the JSON representation for this
|
|
|
|
# object. You can pass the <tt>:full_messages</tt> option. This determines
|
|
|
|
# if the json object should contain full messages or not (false by default).
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.as_json # => {:name=>["cannot be nil"]}
|
|
|
|
# person.errors.as_json(full_messages: true) # => {:name=>["name cannot be nil"]}
|
2016-10-28 23:05:58 -04:00
|
|
|
def as_json(options = nil)
|
2012-02-17 05:12:10 -05:00
|
|
|
to_hash(options && options[:full_messages])
|
|
|
|
end
|
|
|
|
|
2012-12-05 01:11:54 -05:00
|
|
|
# Returns a Hash of attributes with their error messages. If +full_messages+
|
2012-06-22 17:04:16 -04:00
|
|
|
# is +true+, it will contain full messages (see +full_message+).
|
|
|
|
#
|
2014-01-03 17:02:31 -05:00
|
|
|
# person.errors.to_hash # => {:name=>["cannot be nil"]}
|
|
|
|
# person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
|
2012-02-17 05:12:10 -05:00
|
|
|
def to_hash(full_messages = false)
|
2018-03-20 10:39:44 -04:00
|
|
|
hash = {}
|
2018-03-26 01:09:59 -04:00
|
|
|
message_method = full_messages ? :full_message : :message
|
|
|
|
group_by_attribute.each do |attribute, errors|
|
|
|
|
hash[attribute] = errors.map(&message_method)
|
2018-03-20 10:39:44 -04:00
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
alias :messages :to_hash
|
|
|
|
|
|
|
|
def details
|
|
|
|
hash = {}
|
2018-03-26 01:09:59 -04:00
|
|
|
group_by_attribute.each do |attribute, errors|
|
|
|
|
hash[attribute] = errors.map(&:detail)
|
2012-02-17 05:12:10 -05:00
|
|
|
end
|
2018-03-20 10:39:44 -04:00
|
|
|
hash
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2009-03-19 19:28:59 -04:00
|
|
|
|
2018-03-26 01:09:59 -04:00
|
|
|
def group_by_attribute
|
2018-04-02 10:40:09 -04:00
|
|
|
@errors.group_by(&:attribute)
|
2018-03-26 01:09:59 -04:00
|
|
|
end
|
|
|
|
|
2015-01-04 03:18:03 -05:00
|
|
|
# Adds +message+ to the error messages and used validator type to +details+ on +attribute+.
|
|
|
|
# More than one error can be added to the same +attribute+.
|
|
|
|
# If no +message+ is supplied, <tt>:invalid</tt> is assumed.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-06-22 17:04:16 -04:00
|
|
|
# person.errors.add(:name)
|
|
|
|
# # => ["is invalid"]
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:name, :not_implemented, message: "must be implemented")
|
2012-06-22 17:04:16 -04:00
|
|
|
# # => ["is invalid", "must be implemented"]
|
|
|
|
#
|
|
|
|
# person.errors.messages
|
2015-01-04 03:18:03 -05:00
|
|
|
# # => {:name=>["is invalid", "must be implemented"]}
|
|
|
|
#
|
|
|
|
# person.errors.details
|
|
|
|
# # => {:name=>[{error: :not_implemented}, {error: :invalid}]}
|
2012-06-22 17:04:16 -04:00
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# If +message+ is a symbol, it will be translated using the appropriate
|
|
|
|
# scope (see +generate_message+).
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# If +message+ is a proc, it will be called, allowing for things like
|
|
|
|
# <tt>Time.now</tt> to be used within an error.
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
2013-12-24 06:39:41 -05:00
|
|
|
# If the <tt>:strict</tt> option is set to +true+, it will raise
|
2012-06-23 15:49:22 -04:00
|
|
|
# ActiveModel::StrictValidationFailed instead of adding the error.
|
2012-08-06 06:45:27 -04:00
|
|
|
# <tt>:strict</tt> option can also be set to any other exception.
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
2014-12-19 12:26:41 -05:00
|
|
|
# person.errors.add(:name, :invalid, strict: true)
|
2016-05-01 14:41:57 -04:00
|
|
|
# # => ActiveModel::StrictValidationFailed: Name is invalid
|
2014-12-19 12:26:41 -05:00
|
|
|
# person.errors.add(:name, :invalid, strict: NameIsInvalid)
|
2016-05-01 14:41:57 -04:00
|
|
|
# # => NameIsInvalid: Name is invalid
|
2012-06-23 15:49:22 -04:00
|
|
|
#
|
|
|
|
# person.errors.messages # => {}
|
2014-03-27 16:13:56 -04:00
|
|
|
#
|
|
|
|
# +attribute+ should be set to <tt>:base</tt> if the error is not
|
|
|
|
# directly associated with a single attribute.
|
|
|
|
#
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.add(:base, :name_or_email_blank,
|
|
|
|
# message: "either name or email must be present")
|
2014-03-27 16:13:56 -04:00
|
|
|
# person.errors.messages
|
|
|
|
# # => {:base=>["either name or email must be present"]}
|
2015-01-04 03:18:03 -05:00
|
|
|
# person.errors.details
|
|
|
|
# # => {:base=>[{error: :name_or_email_blank}]}
|
2018-03-20 10:39:44 -04:00
|
|
|
def add(attribute, type = :invalid, **options)
|
|
|
|
error = Error.new(
|
|
|
|
@base,
|
|
|
|
*normalize_arguments(attribute, type, options)
|
|
|
|
)
|
|
|
|
|
2012-08-06 06:45:27 -04:00
|
|
|
if exception = options[:strict]
|
|
|
|
exception = ActiveModel::StrictValidationFailed if exception == true
|
2018-03-20 10:39:44 -04:00
|
|
|
raise exception, error.full_message
|
2011-08-17 10:26:00 -04:00
|
|
|
end
|
2010-05-19 10:25:46 -04:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors.append(error)
|
|
|
|
|
|
|
|
error
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
|
|
|
|
2012-06-22 12:52:57 -04:00
|
|
|
# Returns +true+ if an error on the attribute with the given message is
|
2016-06-22 16:45:11 -04:00
|
|
|
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
|
2012-06-22 12:52:57 -04:00
|
|
|
#
|
|
|
|
# person.errors.add :name, :blank
|
2016-06-25 10:10:04 -04:00
|
|
|
# person.errors.added? :name, :blank # => true
|
2016-06-22 16:45:11 -04:00
|
|
|
# person.errors.added? :name, "can't be blank" # => true
|
|
|
|
#
|
2019-01-04 13:35:17 -05:00
|
|
|
# If the error message requires options, then it returns +true+ with
|
|
|
|
# the correct options, or +false+ with incorrect or missing options.
|
|
|
|
#
|
|
|
|
# person.errors.add :name, :too_long, { count: 25 }
|
|
|
|
# person.errors.added? :name, :too_long, count: 25 # => true
|
|
|
|
# person.errors.added? :name, "is too long (maximum is 25 characters)" # => true
|
|
|
|
# person.errors.added? :name, :too_long, count: 24 # => false
|
|
|
|
# person.errors.added? :name, :too_long # => false
|
|
|
|
# person.errors.added? :name, "is too long" # => false
|
2018-03-20 10:39:44 -04:00
|
|
|
def added?(attribute, type = :invalid, options = {})
|
|
|
|
attribute, type, options = normalize_arguments(attribute, type, options)
|
2018-11-09 10:45:18 -05:00
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
if type.is_a? Symbol
|
|
|
|
@errors.any? { |error|
|
|
|
|
error.strict_match?(attribute, type, options)
|
|
|
|
}
|
2017-11-10 03:38:05 -05:00
|
|
|
else
|
2018-03-20 10:39:44 -04:00
|
|
|
messages_for(attribute).include?(type)
|
2017-11-10 03:38:05 -05:00
|
|
|
end
|
2011-10-19 12:47:28 -04:00
|
|
|
end
|
|
|
|
|
2019-01-04 13:35:17 -05:00
|
|
|
# Returns +true+ if an error on the attribute with the given message is
|
|
|
|
# present, or +false+ otherwise. +message+ is treated the same as for +add+.
|
|
|
|
#
|
|
|
|
# person.errors.add :age
|
|
|
|
# person.errors.add :name, :too_long, { count: 25 }
|
|
|
|
# person.errors.of_kind? :age # => true
|
|
|
|
# person.errors.of_kind? :name # => false
|
|
|
|
# person.errors.of_kind? :name, :too_long # => true
|
|
|
|
# person.errors.of_kind? :name, "is too long (maximum is 25 characters)" # => true
|
|
|
|
# person.errors.of_kind? :name, :not_too_long # => false
|
|
|
|
# person.errors.of_kind? :name, "is too long" # => false
|
|
|
|
def of_kind?(attribute, message = :invalid)
|
2018-03-20 10:39:44 -04:00
|
|
|
attribute, message = normalize_arguments(attribute, message)
|
2019-01-04 13:35:17 -05:00
|
|
|
|
|
|
|
if message.is_a? Symbol
|
2018-03-20 10:39:44 -04:00
|
|
|
!where(attribute, message).empty?
|
2019-01-04 13:35:17 -05:00
|
|
|
else
|
2018-03-20 10:39:44 -04:00
|
|
|
messages_for(attribute).include?(message)
|
2019-01-04 13:35:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-19 19:28:59 -04:00
|
|
|
# Returns all the full error messages in an array.
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# class Person
|
2009-03-19 19:28:59 -04:00
|
|
|
# validates_presence_of :name, :address, :email
|
2012-06-22 12:52:57 -04:00
|
|
|
# validates_length_of :name, in: 5..30
|
2009-03-19 19:28:59 -04:00
|
|
|
# end
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# person = Person.create(address: '123 First St.')
|
|
|
|
# person.errors.full_messages
|
|
|
|
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
|
2009-10-20 20:20:01 -04:00
|
|
|
def full_messages
|
2018-03-20 10:39:44 -04:00
|
|
|
@errors.map(&:full_message)
|
2011-09-09 04:28:25 -04:00
|
|
|
end
|
2015-02-20 15:31:24 -05:00
|
|
|
alias :to_a :full_messages
|
2009-10-20 20:20:01 -04:00
|
|
|
|
2013-03-24 03:34:27 -04:00
|
|
|
# Returns all the full error messages for a given attribute in an array.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# validates_presence_of :name, :email
|
|
|
|
# validates_length_of :name, in: 5..30
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.create()
|
|
|
|
# person.errors.full_messages_for(:name)
|
|
|
|
# # => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
|
|
|
|
def full_messages_for(attribute)
|
2018-03-20 10:39:44 -04:00
|
|
|
where(attribute).map(&:full_message).freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def messages_for(attribute)
|
|
|
|
where(attribute).map(&:message).freeze
|
2013-03-24 03:34:27 -04:00
|
|
|
end
|
|
|
|
|
2011-09-09 04:28:25 -04:00
|
|
|
# Returns a full message for a given attribute.
|
|
|
|
#
|
2012-06-22 12:52:57 -04:00
|
|
|
# person.errors.full_message(:name, 'is invalid') # => "Name is invalid"
|
2011-09-09 04:28:25 -04:00
|
|
|
def full_message(attribute, message)
|
|
|
|
return message if attribute == :base
|
2018-08-14 11:52:26 -04:00
|
|
|
attribute = attribute.to_s
|
2018-05-17 08:39:31 -04:00
|
|
|
|
2019-03-29 12:08:48 -04:00
|
|
|
if self.class.i18n_customize_full_message && @base.class.respond_to?(:i18n_scope)
|
2018-08-14 11:52:26 -04:00
|
|
|
attribute = attribute.remove(/\[\d\]/)
|
|
|
|
parts = attribute.split(".")
|
2018-05-17 08:39:31 -04:00
|
|
|
attribute_name = parts.pop
|
|
|
|
namespace = parts.join("/") unless parts.empty?
|
|
|
|
attributes_scope = "#{@base.class.i18n_scope}.errors.models"
|
|
|
|
|
|
|
|
if namespace
|
|
|
|
defaults = @base.class.lookup_ancestors.map do |klass|
|
|
|
|
[
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.attributes.#{attribute_name}.format",
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}/#{namespace}.format",
|
|
|
|
]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
defaults = @base.class.lookup_ancestors.map do |klass|
|
|
|
|
[
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.attributes.#{attribute_name}.format",
|
|
|
|
:"#{attributes_scope}.#{klass.model_name.i18n_key}.format",
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults.flatten!
|
|
|
|
else
|
|
|
|
defaults = []
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults << :"errors.format"
|
|
|
|
defaults << "%{attribute} %{message}"
|
|
|
|
|
2018-08-14 11:52:26 -04:00
|
|
|
attr_name = attribute.tr(".", "_").humanize
|
2018-08-16 11:07:56 -04:00
|
|
|
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
|
2018-08-14 11:52:26 -04:00
|
|
|
|
2018-05-17 08:39:31 -04:00
|
|
|
I18n.t(defaults.shift,
|
|
|
|
default: defaults,
|
2013-05-01 20:10:06 -04:00
|
|
|
attribute: attr_name,
|
2016-08-06 13:44:11 -04:00
|
|
|
message: message)
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Translates an error message in its default scope
|
2010-06-14 05:10:57 -04:00
|
|
|
# (<tt>activemodel.errors.messages</tt>).
|
|
|
|
#
|
2015-01-12 10:21:56 -05:00
|
|
|
# Error messages are first looked up in <tt>activemodel.errors.models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>,
|
|
|
|
# if it's not there, it's looked up in <tt>activemodel.errors.models.MODEL.MESSAGE</tt> and if
|
2012-06-22 12:52:57 -04:00
|
|
|
# that is not there also, it returns the translation of the default message
|
|
|
|
# (e.g. <tt>activemodel.errors.messages.MESSAGE</tt>). The translated model
|
|
|
|
# name, translated attribute name and the value are available for
|
|
|
|
# interpolation.
|
2009-03-19 19:28:59 -04:00
|
|
|
#
|
2010-06-15 04:04:22 -04:00
|
|
|
# When using inheritance in your models, it will check all the inherited
|
2010-06-14 05:10:57 -04:00
|
|
|
# models too, but only if the model itself hasn't been found. Say you have
|
2010-08-14 01:13:00 -04:00
|
|
|
# <tt>class Admin < User; end</tt> and you wanted the translation for
|
2011-03-29 01:31:05 -04:00
|
|
|
# the <tt>:blank</tt> error message for the <tt>title</tt> attribute,
|
2010-06-14 05:10:57 -04:00
|
|
|
# it looks for these translations:
|
2009-06-08 21:32:08 -04:00
|
|
|
#
|
2011-03-29 01:31:05 -04:00
|
|
|
# * <tt>activemodel.errors.models.admin.attributes.title.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.admin.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.user.attributes.title.blank</tt>
|
|
|
|
# * <tt>activemodel.errors.models.user.blank</tt>
|
|
|
|
# * any default you provided through the +options+ hash (in the <tt>activemodel.errors</tt> scope)
|
|
|
|
# * <tt>activemodel.errors.messages.blank</tt>
|
|
|
|
# * <tt>errors.attributes.title.blank</tt>
|
|
|
|
# * <tt>errors.messages.blank</tt>
|
2010-05-19 10:25:46 -04:00
|
|
|
def generate_message(attribute, type = :invalid, options = {})
|
|
|
|
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
2019-02-27 09:42:02 -05:00
|
|
|
value = (attribute != :base ? @base.send(:read_attribute_for_validation, attribute) : nil)
|
|
|
|
|
|
|
|
options = {
|
|
|
|
model: @base.model_name.human,
|
|
|
|
attribute: @base.class.human_attribute_name(attribute),
|
|
|
|
value: value,
|
|
|
|
object: @base
|
|
|
|
}.merge!(options)
|
2010-05-19 10:25:46 -04:00
|
|
|
|
2011-10-11 15:44:22 -04:00
|
|
|
if @base.class.respond_to?(:i18n_scope)
|
2017-08-18 07:35:43 -04:00
|
|
|
i18n_scope = @base.class.i18n_scope.to_s
|
|
|
|
defaults = @base.class.lookup_ancestors.flat_map do |klass|
|
|
|
|
[ :"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
|
|
|
:"#{i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
2011-10-11 15:44:22 -04:00
|
|
|
end
|
2017-08-18 07:35:43 -04:00
|
|
|
defaults << :"#{i18n_scope}.errors.messages.#{type}"
|
2019-02-27 09:42:02 -05:00
|
|
|
|
|
|
|
catch(:exception) do
|
|
|
|
translation = I18n.translate(defaults.first, options.merge(default: defaults.drop(1), throw: true))
|
|
|
|
return translation unless translation.nil?
|
|
|
|
end unless options[:message]
|
2011-10-11 15:44:22 -04:00
|
|
|
else
|
|
|
|
defaults = []
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2009-03-20 17:40:37 -04:00
|
|
|
|
2010-05-19 10:25:46 -04:00
|
|
|
defaults << :"errors.attributes.#{attribute}.#{type}"
|
|
|
|
defaults << :"errors.messages.#{type}"
|
2010-01-30 07:07:48 -05:00
|
|
|
|
2009-03-19 19:28:59 -04:00
|
|
|
key = defaults.shift
|
2015-05-04 08:36:26 -04:00
|
|
|
defaults = options.delete(:message) if options[:message]
|
2019-02-27 09:42:02 -05:00
|
|
|
options[:default] = defaults
|
2009-03-19 19:28:59 -04:00
|
|
|
|
|
|
|
I18n.translate(key, options)
|
|
|
|
end
|
2011-10-19 12:47:28 -04:00
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
def marshal_load(array) # :nodoc:
|
2018-03-20 10:39:44 -04:00
|
|
|
# Rails 5
|
|
|
|
@errors = []
|
|
|
|
@base = array[0]
|
|
|
|
add_from_legacy_details_hash(array[2])
|
2016-05-30 14:00:14 -04:00
|
|
|
end
|
|
|
|
|
2016-12-08 16:20:07 -05:00
|
|
|
def init_with(coder) # :nodoc:
|
2018-03-20 10:39:44 -04:00
|
|
|
data = coder.map
|
|
|
|
|
|
|
|
data.each { |k, v|
|
|
|
|
next if LEGACY_ATTRIBUTES.include?(k.to_sym)
|
|
|
|
instance_variable_set(:"@#{k}", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
@errors ||= []
|
|
|
|
|
|
|
|
# Legacy support Rails 5.x details hash
|
|
|
|
add_from_legacy_details_hash(data["details"]) if data.key?("details")
|
2016-12-08 16:20:07 -05:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def normalize_arguments(attribute, type, **options)
|
|
|
|
# Evaluate proc first
|
|
|
|
if type.respond_to?(:call)
|
|
|
|
type = type.call(@base, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
[attribute.to_sym, type, options]
|
2016-05-30 14:00:14 -04:00
|
|
|
end
|
|
|
|
|
2018-03-20 10:39:44 -04:00
|
|
|
def add_from_legacy_details_hash(details)
|
|
|
|
details.each { |attribute, errors|
|
|
|
|
errors.each { |error|
|
|
|
|
type = error.delete(:error)
|
|
|
|
add(attribute, type, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def deprecation_removal_warning(method_name)
|
|
|
|
ActiveSupport::Deprecation.warn("ActiveModel::Errors##{method_name} is deprecated and will be removed in Rails 6.1")
|
|
|
|
end
|
2018-04-02 22:52:36 -04:00
|
|
|
|
|
|
|
def deprecation_rename_warning(old_method_name, new_method_name)
|
|
|
|
ActiveSupport::Deprecation.warn("ActiveModel::Errors##{old_method_name} is deprecated. Please call ##{new_method_name} instead.")
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2011-08-17 10:26:00 -04:00
|
|
|
|
2012-06-23 15:49:22 -04:00
|
|
|
# Raised when a validation cannot be corrected by end users and are considered
|
2012-06-25 03:23:33 -04:00
|
|
|
# exceptional.
|
2012-07-28 15:24:56 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
#
|
|
|
|
# validates_presence_of :name, strict: true
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = nil
|
|
|
|
# person.valid?
|
|
|
|
# # => ActiveModel::StrictValidationFailed: Name can't be blank
|
2011-08-17 10:26:00 -04:00
|
|
|
class StrictValidationFailed < StandardError
|
|
|
|
end
|
2015-02-25 09:14:43 -05:00
|
|
|
|
2016-05-03 14:37:11 -04:00
|
|
|
# Raised when attribute values are out of range.
|
|
|
|
class RangeError < ::RangeError
|
|
|
|
end
|
|
|
|
|
2015-02-25 09:14:43 -05:00
|
|
|
# Raised when unknown attributes are supplied via mass assignment.
|
2016-05-01 17:38:59 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeAssignment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.assign_attributes(name: 'Gorby')
|
|
|
|
# # => ActiveModel::UnknownAttributeError: unknown attribute 'name' for Person.
|
2015-02-25 09:14:43 -05:00
|
|
|
class UnknownAttributeError < NoMethodError
|
|
|
|
attr_reader :record, :attribute
|
|
|
|
|
|
|
|
def initialize(record, attribute)
|
|
|
|
@record = record
|
|
|
|
@attribute = attribute
|
|
|
|
super("unknown attribute '#{attribute}' for #{@record.class}.")
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|