2009-06-08 21:32:08 -04:00
|
|
|
require 'active_support/core_ext/array/extract_options'
|
|
|
|
require 'active_support/core_ext/hash/keys'
|
2010-08-05 15:09:57 -04:00
|
|
|
require 'active_support/core_ext/hash/except'
|
2009-06-08 21:32:08 -04:00
|
|
|
|
2007-11-09 09:59:15 -05:00
|
|
|
module ActiveModel
|
2010-06-14 05:26:51 -04:00
|
|
|
|
2013-12-25 08:20:44 -05:00
|
|
|
# == Active \Model \Validations
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# Provides a full validation framework to your objects.
|
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
|
|
|
|
# include ActiveModel::Validations
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :first_name, :last_name
|
|
|
|
#
|
|
|
|
# validates_each :first_name, :last_name do |record, attr, value|
|
|
|
|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# Which provides you with the full standard validation stack that you
|
2010-07-29 20:30:04 -04:00
|
|
|
# know from Active Record:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# person = Person.new
|
2010-07-29 20:30:04 -04:00
|
|
|
# person.valid? # => true
|
|
|
|
# person.invalid? # => false
|
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# person.first_name = 'zoolander'
|
2010-07-29 20:30:04 -04:00
|
|
|
# person.valid? # => false
|
|
|
|
# person.invalid? # => true
|
2012-10-22 13:34:24 -04:00
|
|
|
# person.errors.messages # => {first_name:["starts with z."]}
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-07-29 20:52:25 -04:00
|
|
|
# Note that <tt>ActiveModel::Validations</tt> automatically adds an +errors+
|
|
|
|
# method to your instances initialized with a new <tt>ActiveModel::Errors</tt>
|
|
|
|
# object, so there is no need for you to do this manually.
|
2007-11-09 09:59:15 -05:00
|
|
|
module Validations
|
2009-05-29 18:23:38 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2011-12-28 10:38:16 -05:00
|
|
|
extend ActiveModel::Callbacks
|
2009-10-20 20:20:01 -04:00
|
|
|
extend ActiveModel::Translation
|
2010-05-11 06:28:42 -04:00
|
|
|
|
2010-05-11 09:36:09 -04:00
|
|
|
extend HelperMethods
|
|
|
|
include HelperMethods
|
2010-05-11 06:28:42 -04:00
|
|
|
|
2010-05-08 16:21:44 -04:00
|
|
|
attr_accessor :validation_context
|
2013-05-01 20:10:06 -04:00
|
|
|
define_callbacks :validate, scope: :name
|
2010-05-08 16:21:44 -04:00
|
|
|
|
2012-05-25 10:58:16 -04:00
|
|
|
class_attribute :_validators
|
2010-02-18 10:28:48 -05:00
|
|
|
self._validators = Hash.new { |h,k| h[k] = [] }
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2009-12-22 19:37:19 -05:00
|
|
|
# Validates each attribute against a block.
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :first_name, :last_name
|
|
|
|
#
|
2012-07-29 20:52:25 -04:00
|
|
|
# validates_each :first_name, :last_name, allow_blank: true do |record, attr, value|
|
2010-01-31 18:08:20 -05:00
|
|
|
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
2009-12-22 19:37:19 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Options:
|
2014-01-18 11:34:38 -05:00
|
|
|
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
|
|
|
# You can pass a symbol or an array of symbols.
|
|
|
|
# (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt> or
|
|
|
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
2009-12-22 19:37:19 -05:00
|
|
|
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
|
|
|
|
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
|
2010-06-14 05:26:51 -04:00
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
2012-07-29 20:52:25 -04:00
|
|
|
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
|
|
|
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
|
|
|
# proc or string should return or evaluate to a +true+ or +false+ value.
|
|
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
|
|
|
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
|
|
|
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
|
|
|
# method, proc or string should return or evaluate to a +true+ or +false+
|
|
|
|
# value.
|
2009-12-22 19:37:19 -05:00
|
|
|
def validates_each(*attr_names, &block)
|
2012-06-25 13:24:46 -04:00
|
|
|
validates_with BlockValidator, _merge_attributes(attr_names), &block
|
2009-12-22 19:37:19 -05:00
|
|
|
end
|
|
|
|
|
2008-03-31 20:13:39 -04:00
|
|
|
# Adds a validation method or block to the class. This is useful when
|
2010-06-11 06:15:34 -04:00
|
|
|
# overriding the +validate+ instance method becomes too unwieldy and
|
2008-03-31 20:13:39 -04:00
|
|
|
# you're looking for more descriptive declaration of your validations.
|
|
|
|
#
|
|
|
|
# This can be done with a symbol pointing to a method:
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Comment
|
|
|
|
# include ActiveModel::Validations
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2008-03-31 20:13:39 -04:00
|
|
|
# validate :must_be_friends
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
2012-07-29 20:52:25 -04:00
|
|
|
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
2008-03-31 20:13:39 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2010-12-19 15:50:18 -05:00
|
|
|
# With a block which is passed with the current record to be validated:
|
2008-03-31 20:13:39 -04:00
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class Comment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
2008-03-31 20:13:39 -04:00
|
|
|
# validate do |comment|
|
|
|
|
# comment.must_be_friends
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
2012-07-29 20:52:25 -04:00
|
|
|
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
2008-03-31 20:13:39 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2010-12-19 15:50:18 -05:00
|
|
|
# Or with a block where self points to the current record to be validated:
|
|
|
|
#
|
|
|
|
# class Comment
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# validate do
|
2012-07-29 20:52:25 -04:00
|
|
|
# errors.add(:base, 'Must be friends to leave a comment') unless commenter.friend_of?(commentee)
|
2010-12-19 15:50:18 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-04-26 04:09:12 -04:00
|
|
|
# Options:
|
2014-01-18 11:34:38 -05:00
|
|
|
# * <tt>:on</tt> - Specifies the contexts where this validation is active.
|
|
|
|
# You can pass a symbol or an array of symbols.
|
|
|
|
# (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt> or
|
|
|
|
# <tt>on: [:create, :custom_validation_context]</tt>)
|
2012-04-26 04:09:12 -04:00
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
2012-07-29 20:52:25 -04:00
|
|
|
# if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
|
|
|
|
# or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
|
|
|
|
# proc or string should return or evaluate to a +true+ or +false+ value.
|
|
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
|
|
|
# determine if the validation should not occur (e.g. <tt>unless: :skip_validation</tt>,
|
|
|
|
# or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
|
|
|
# method, proc or string should return or evaluate to a +true+ or +false+
|
|
|
|
# value.
|
2009-09-08 11:10:14 -04:00
|
|
|
def validate(*args, &block)
|
2010-08-03 09:34:31 -04:00
|
|
|
options = args.extract_options!
|
|
|
|
if options.key?(:on)
|
|
|
|
options = options.dup
|
2012-01-05 17:52:31 -05:00
|
|
|
options[:if] = Array(options[:if])
|
2013-06-11 13:20:29 -04:00
|
|
|
options[:if].unshift lambda { |o|
|
2014-01-18 11:34:38 -05:00
|
|
|
Array(options[:on]).include?(o.validation_context)
|
2013-06-11 13:20:29 -04:00
|
|
|
}
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|
2010-08-03 09:34:31 -04:00
|
|
|
args << options
|
2009-09-08 11:22:45 -04:00
|
|
|
set_callback(:validate, *args, &block)
|
2009-09-08 11:10:14 -04:00
|
|
|
end
|
2010-02-18 10:28:48 -05:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# List all validators that are being used to validate the model using
|
2010-06-14 05:26:51 -04:00
|
|
|
# +validates_with+ method.
|
2012-06-15 01:31:06 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# validates_with MyValidator
|
|
|
|
# validates_with OtherValidator, on: :create
|
|
|
|
# validates_with StrictValidator, strict: true
|
|
|
|
# end
|
|
|
|
#
|
2012-06-22 12:44:29 -04:00
|
|
|
# Person.validators
|
2012-06-15 01:31:06 -04:00
|
|
|
# # => [
|
|
|
|
# # #<MyValidator:0x007fbff403e808 @options={}>,
|
2012-10-22 13:34:24 -04:00
|
|
|
# # #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
|
|
|
|
# # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
|
2012-06-15 01:31:06 -04:00
|
|
|
# # ]
|
2010-02-18 10:28:48 -05:00
|
|
|
def validators
|
|
|
|
_validators.values.flatten.uniq
|
|
|
|
end
|
|
|
|
|
2013-02-21 13:40:48 -05:00
|
|
|
# Clears all of the validators and validations.
|
|
|
|
#
|
|
|
|
# Note that this will clear anything that is being used to validate
|
|
|
|
# the model for both the +validates_with+ and +validate+ methods.
|
|
|
|
# It clears the validators that are created with an invocation of
|
|
|
|
# +validates_with+ and the callbacks that are set by an invocation
|
|
|
|
# of +validate+.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# validates_with MyValidator
|
|
|
|
# validates_with OtherValidator, on: :create
|
|
|
|
# validates_with StrictValidator, strict: true
|
|
|
|
# validate :cannot_be_robot
|
|
|
|
#
|
|
|
|
# def cannot_be_robot
|
|
|
|
# errors.add(:base, 'A person cannot be a robot') if person_is_robot
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Person.validators
|
|
|
|
# # => [
|
|
|
|
# # #<MyValidator:0x007fbff403e808 @options={}>,
|
|
|
|
# # #<OtherValidator:0x007fbff403d930 @options={on: :create}>,
|
|
|
|
# # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
|
|
|
|
# # ]
|
|
|
|
#
|
2014-01-27 05:24:33 -05:00
|
|
|
# If one runs <tt>Person.clear_validators!</tt> and then checks to see what
|
2013-02-21 13:40:48 -05:00
|
|
|
# validators this class has, you would obtain:
|
|
|
|
#
|
|
|
|
# Person.validators # => []
|
|
|
|
#
|
2014-01-27 05:24:33 -05:00
|
|
|
# Also, the callback set by <tt>validate :cannot_be_robot</tt> will be erased
|
2013-02-21 13:40:48 -05:00
|
|
|
# so that:
|
|
|
|
#
|
|
|
|
# Person._validate_callbacks.empty? # => true
|
|
|
|
#
|
|
|
|
def clear_validators!
|
|
|
|
reset_callbacks(:validate)
|
|
|
|
_validators.clear
|
|
|
|
end
|
|
|
|
|
2012-04-26 04:34:51 -04:00
|
|
|
# List all validators that are being used to validate a specific attribute.
|
2012-07-29 20:52:25 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name , :age
|
|
|
|
#
|
|
|
|
# validates_presence_of :name
|
|
|
|
# validates_inclusion_of :age, in: 0..99
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Person.validators_on(:name)
|
|
|
|
# # => [
|
|
|
|
# # #<ActiveModel::Validations::PresenceValidator:0x007fe604914e60 @attributes=[:name], @options={}>,
|
|
|
|
# # ]
|
2011-02-05 23:27:02 -05:00
|
|
|
def validators_on(*attributes)
|
2012-10-02 23:17:10 -04:00
|
|
|
attributes.flat_map do |attribute|
|
2011-02-07 16:15:06 -05:00
|
|
|
_validators[attribute.to_sym]
|
2012-10-02 23:17:10 -04:00
|
|
|
end
|
2010-02-18 10:28:48 -05:00
|
|
|
end
|
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
# Returns +true+ if +attribute+ is an attribute method, +false+ otherwise.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# User.attribute_method?(:name) # => true
|
|
|
|
# User.attribute_method?(:age) # => false
|
2010-04-28 15:24:47 -04:00
|
|
|
def attribute_method?(attribute)
|
|
|
|
method_defined?(attribute)
|
|
|
|
end
|
2010-06-10 13:39:09 -04:00
|
|
|
|
|
|
|
# Copy validators on inheritance.
|
2012-07-29 20:52:25 -04:00
|
|
|
def inherited(base) #:nodoc:
|
2010-06-10 13:39:09 -04:00
|
|
|
dup = _validators.dup
|
|
|
|
base._validators = dup.each { |k, v| dup[k] = v.dup }
|
|
|
|
super
|
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2012-05-15 12:38:29 -04:00
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
# Clean the +Errors+ object if instance is duped.
|
|
|
|
def initialize_dup(other) #:nodoc:
|
2012-05-12 09:03:21 -04:00
|
|
|
@errors = nil
|
|
|
|
super
|
2012-05-15 12:38:29 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
# Returns the +Errors+ object that holds all information about attribute
|
|
|
|
# error messages.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# validates_presence_of :name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.valid? # => false
|
2012-10-22 13:34:24 -04:00
|
|
|
# person.errors # => #<ActiveModel::Errors:0x007fe603816640 @messages={name:["can't be blank"]}>
|
2008-03-31 20:05:48 -04:00
|
|
|
def errors
|
2009-03-19 19:28:59 -04:00
|
|
|
@errors ||= Errors.new(self)
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
2008-03-31 19:40:34 -04:00
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
# Runs all the specified validations and returns +true+ if no errors were
|
|
|
|
# added otherwise +false+.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# validates_presence_of :name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = ''
|
2012-10-18 01:43:58 -04:00
|
|
|
# person.valid? # => false
|
2012-07-29 20:52:25 -04:00
|
|
|
# person.name = 'david'
|
|
|
|
# person.valid? # => true
|
|
|
|
#
|
|
|
|
# Context can optionally be supplied to define which callbacks to test
|
|
|
|
# against (the context is defined on the validations using <tt>:on</tt>).
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# validates_presence_of :name, on: :new
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
2012-10-18 01:43:58 -04:00
|
|
|
# person.valid? # => true
|
2012-07-29 20:52:25 -04:00
|
|
|
# person.valid?(:new) # => false
|
2010-05-08 16:21:44 -04:00
|
|
|
def valid?(context = nil)
|
|
|
|
current_context, self.validation_context = validation_context, context
|
2008-03-31 19:40:34 -04:00
|
|
|
errors.clear
|
2010-06-19 12:18:45 -04:00
|
|
|
run_validations!
|
2010-05-08 16:21:44 -04:00
|
|
|
ensure
|
|
|
|
self.validation_context = current_context
|
2008-03-31 19:40:34 -04:00
|
|
|
end
|
2009-03-19 19:28:59 -04:00
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
|
|
|
|
# added, +false+ otherwise.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# validates_presence_of :name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = ''
|
2012-10-18 01:43:58 -04:00
|
|
|
# person.invalid? # => true
|
2012-07-29 20:52:25 -04:00
|
|
|
# person.name = 'david'
|
|
|
|
# person.invalid? # => false
|
|
|
|
#
|
|
|
|
# Context can optionally be supplied to define which callbacks to test
|
|
|
|
# against (the context is defined on the validations using <tt>:on</tt>).
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# validates_presence_of :name, on: :new
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
2012-10-18 01:43:58 -04:00
|
|
|
# person.invalid? # => false
|
2012-07-29 20:52:25 -04:00
|
|
|
# person.invalid?(:new) # => true
|
2010-05-08 16:21:44 -04:00
|
|
|
def invalid?(context = nil)
|
|
|
|
!valid?(context)
|
2009-03-19 19:28:59 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
# Hook method defining how an attribute value should be retrieved. By default
|
|
|
|
# this is assumed to be an instance named after the attribute. Override this
|
2010-06-14 05:26:51 -04:00
|
|
|
# method in subclasses should you need to retrieve the value for a given
|
|
|
|
# attribute differently:
|
|
|
|
#
|
2010-01-07 12:44:35 -05:00
|
|
|
# class MyClass
|
|
|
|
# include ActiveModel::Validations
|
|
|
|
#
|
|
|
|
# def initialize(data = {})
|
|
|
|
# @data = data
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def read_attribute_for_validation(key)
|
|
|
|
# @data[key]
|
|
|
|
# end
|
|
|
|
# end
|
2010-01-07 13:22:32 -05:00
|
|
|
alias :read_attribute_for_validation :send
|
2010-06-19 12:18:45 -04:00
|
|
|
|
|
|
|
protected
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-07-29 20:52:25 -04:00
|
|
|
def run_validations! #:nodoc:
|
2011-01-09 13:15:05 -05:00
|
|
|
run_callbacks :validate
|
2010-06-19 12:18:45 -04:00
|
|
|
errors.empty?
|
|
|
|
end
|
2007-11-09 09:59:15 -05:00
|
|
|
end
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
|
|
|
|
2013-03-20 14:24:48 -04:00
|
|
|
Dir[File.dirname(__FILE__) + "/validations/*.rb"].each { |file| require file }
|