2008-03-31 20:05:48 -04:00
|
|
|
module ActiveModel
|
2012-05-16 11:01:43 -04:00
|
|
|
|
2010-06-15 13:47:06 -04:00
|
|
|
# == Active Model Confirmation Validator
|
2008-03-31 20:05:48 -04:00
|
|
|
module Validations
|
2012-06-25 13:14:50 -04:00
|
|
|
class ConfirmationValidator < EachValidator #:nodoc:
|
2009-12-22 18:36:51 -05:00
|
|
|
def validate_each(record, attribute, value)
|
2010-08-31 17:57:39 -04:00
|
|
|
if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
|
2012-04-24 01:05:41 -04:00
|
|
|
human_attribute_name = record.class.human_attribute_name(attribute)
|
|
|
|
record.errors.add(:"#{attribute}_confirmation", :confirmation, options.merge(:attribute => human_attribute_name))
|
2010-08-31 17:57:39 -04:00
|
|
|
end
|
2009-12-22 18:36:51 -05:00
|
|
|
end
|
2010-05-19 10:25:46 -04:00
|
|
|
|
2010-01-07 12:44:35 -05:00
|
|
|
def setup(klass)
|
2010-09-24 11:53:51 -04:00
|
|
|
klass.send(:attr_accessor, *attributes.map do |attribute|
|
|
|
|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation")
|
|
|
|
end.compact)
|
2010-01-07 12:44:35 -05:00
|
|
|
end
|
2009-12-22 18:36:51 -05:00
|
|
|
end
|
|
|
|
|
2010-05-11 06:28:42 -04:00
|
|
|
module HelperMethods
|
2010-08-14 01:13:00 -04:00
|
|
|
# Encapsulates the pattern of wanting to validate a password or email
|
2012-05-16 02:23:46 -04:00
|
|
|
# address field with a confirmation.
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# Model:
|
|
|
|
# class Person < ActiveRecord::Base
|
|
|
|
# validates_confirmation_of :user_name, :password
|
2010-08-14 01:13:00 -04:00
|
|
|
# validates_confirmation_of :email_address,
|
2012-07-05 01:09:29 -04:00
|
|
|
# message: 'should match confirmation'
|
2008-03-31 20:05:48 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# View:
|
|
|
|
# <%= password_field "person", "password" %>
|
|
|
|
# <%= password_field "person", "password_confirmation" %>
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# The added +password_confirmation+ attribute is virtual; it exists only
|
2010-06-15 13:47:06 -04:00
|
|
|
# as an in-memory attribute for validating the password. To achieve this,
|
2010-08-14 01:13:00 -04:00
|
|
|
# the validation adds accessors to the model for the confirmation
|
2010-06-15 13:47:06 -04:00
|
|
|
# attribute.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
|
|
|
# NOTE: This check is performed only if +password_confirmation+ is not
|
2012-07-05 01:09:29 -04:00
|
|
|
# +nil+. To require confirmation, make sure to add a presence check for
|
|
|
|
# the confirmation attribute:
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
2012-07-05 01:09:29 -04:00
|
|
|
# validates_presence_of :password_confirmation, if: :password_changed?
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# Configuration options:
|
2010-06-15 13:47:06 -04:00
|
|
|
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
|
|
|
|
# confirmation").
|
2011-02-21 05:37:08 -05:00
|
|
|
# * <tt>:on</tt> - Specifies when this validation is active. Runs in all
|
|
|
|
# validation contexts by default (+nil+), other options are <tt>:create</tt>
|
|
|
|
# and <tt>:update</tt>.
|
2010-06-15 13:47:06 -04:00
|
|
|
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine
|
2012-07-05 01:09:29 -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.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * <tt>:unless</tt> - Specifies a method, proc or string to call to
|
|
|
|
# determine if the validation should not occur (e.g.
|
2012-07-05 01:09:29 -04:00
|
|
|
# <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.
|
2012-06-25 13:14:50 -04:00
|
|
|
# * <tt>:strict</tt> - Specifies whether validation should be strict.
|
2012-05-16 02:23:46 -04:00
|
|
|
# See <tt>ActiveModel::Validation#validates!</tt> for more information.
|
2008-03-31 20:05:48 -04:00
|
|
|
def validates_confirmation_of(*attr_names)
|
2010-01-07 12:44:35 -05:00
|
|
|
validates_with ConfirmationValidator, _merge_attributes(attr_names)
|
2008-03-31 20:05:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-08 21:32:08 -04:00
|
|
|
end
|