2007-11-09 14:59:15 +00:00
|
|
|
module ActiveModel
|
|
|
|
module Validations
|
2009-05-29 22:23:38 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
included do
|
|
|
|
define_callbacks :validate
|
2008-03-31 23:40:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2008-04-01 00:13:39 +00:00
|
|
|
# Adds a validation method or block to the class. This is useful when
|
2008-05-02 13:45:23 +00:00
|
|
|
# overriding the +validate+ instance method becomes too unwieldly and
|
2008-04-01 00:13:39 +00:00
|
|
|
# you're looking for more descriptive declaration of your validations.
|
|
|
|
#
|
|
|
|
# This can be done with a symbol pointing to a method:
|
|
|
|
#
|
|
|
|
# class Comment < ActiveRecord::Base
|
|
|
|
# validate :must_be_friends
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
|
|
|
# errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Or with a block which is passed the current record to be validated:
|
|
|
|
#
|
|
|
|
# class Comment < ActiveRecord::Base
|
|
|
|
# validate do |comment|
|
|
|
|
# comment.must_be_friends
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def must_be_friends
|
|
|
|
# errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2008-05-02 13:45:23 +00:00
|
|
|
# This usage applies to +validate_on_create+ and +validate_on_update as well+.
|
2009-03-19 23:28:59 +00:00
|
|
|
|
2008-04-01 00:13:39 +00:00
|
|
|
# Validates each attribute against a block.
|
|
|
|
#
|
|
|
|
# class Person < ActiveRecord::Base
|
|
|
|
# validates_each :first_name, :last_name do |record, attr, value|
|
|
|
|
# record.errors.add attr, 'starts with z.' if value[0] == ?z
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Options:
|
2009-03-19 23:28:59 +00:00
|
|
|
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
|
2008-05-02 13:45:23 +00:00
|
|
|
# * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
|
|
|
|
# * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
|
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
|
|
|
|
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
|
2008-04-01 00:13:39 +00:00
|
|
|
# method, proc or string should return or evaluate to a true or false value.
|
2008-05-02 13:45:23 +00:00
|
|
|
# * <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
|
2008-04-01 00:13:39 +00:00
|
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
|
|
def validates_each(*attrs)
|
|
|
|
options = attrs.extract_options!.symbolize_keys
|
|
|
|
attrs = attrs.flatten
|
|
|
|
|
|
|
|
# Declare the validation.
|
2009-03-21 01:11:38 +00:00
|
|
|
send(validation_method(options[:on]), options) do |record|
|
2008-04-01 00:13:39 +00:00
|
|
|
attrs.each do |attr|
|
2009-03-20 21:40:37 +00:00
|
|
|
value = record.get_attribute_value(attr)
|
2008-04-01 00:13:39 +00:00
|
|
|
next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
|
|
|
|
yield record, attr, value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-03-31 23:40:34 +00:00
|
|
|
|
|
|
|
private
|
2009-03-20 21:40:37 +00:00
|
|
|
|
|
|
|
def validation_method(on)
|
2009-03-21 01:11:38 +00:00
|
|
|
:validate
|
2009-03-20 21:40:37 +00:00
|
|
|
end
|
2008-03-31 23:40:34 +00:00
|
|
|
end
|
|
|
|
|
2008-04-01 00:05:48 +00:00
|
|
|
# Returns the Errors object that holds all information about attribute error messages.
|
|
|
|
def errors
|
2009-03-19 23:28:59 +00:00
|
|
|
@errors ||= Errors.new(self)
|
2008-04-01 00:05:48 +00:00
|
|
|
end
|
2008-03-31 23:40:34 +00:00
|
|
|
|
2008-04-01 00:05:48 +00:00
|
|
|
# Runs all the specified validations and returns true if no errors were added otherwise false.
|
2008-03-31 23:40:34 +00:00
|
|
|
def valid?
|
|
|
|
errors.clear
|
|
|
|
run_callbacks(:validate)
|
|
|
|
errors.empty?
|
|
|
|
end
|
2009-03-19 23:28:59 +00:00
|
|
|
|
|
|
|
# Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
|
|
|
|
def invalid?
|
|
|
|
!valid?
|
|
|
|
end
|
|
|
|
|
2009-03-20 21:40:37 +00:00
|
|
|
def get_attribute_value(attribute)
|
|
|
|
respond_to?(attribute.to_sym) ? send(attribute.to_sym) : instance_variable_get(:"@#{attribute}")
|
|
|
|
end
|
2007-11-09 14:59:15 +00:00
|
|
|
end
|
2008-04-01 00:05:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
|
|
|
|
filename = File.basename(path)
|
|
|
|
require "active_model/validations/#{filename}"
|
2009-03-19 23:28:59 +00:00
|
|
|
end
|