2008-03-31 20:05:48 -04:00
|
|
|
module ActiveModel
|
2010-06-15 13:47:06 -04:00
|
|
|
|
|
|
|
# == Active Model Confirmation Validator
|
2008-03-31 20:05:48 -04:00
|
|
|
module Validations
|
2009-12-22 18:36:51 -05:00
|
|
|
class ConfirmationValidator < EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
confirmed = record.send(:"#{attribute}_confirmation")
|
|
|
|
return if confirmed.nil? || value == confirmed
|
2010-05-19 10:25:46 -04:00
|
|
|
record.errors.add(attribute, :confirmation, options)
|
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-05-19 10:25:46 -04:00
|
|
|
klass.send(:attr_accessor, *attributes.map { |attribute| :"#{attribute}_confirmation" })
|
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
|
2010-06-15 13:47:06 -04:00
|
|
|
# address field with a confirmation. For example:
|
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,
|
2010-06-15 13:47:06 -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
|
2010-06-15 13:47:06 -04:00
|
|
|
# +nil+, and by default only on save. To require confirmation, make sure
|
|
|
|
# to add a presence check for the confirmation attribute:
|
2008-03-31 20:05:48 -04:00
|
|
|
#
|
|
|
|
# validates_presence_of :password_confirmation, :if => :password_changed?
|
|
|
|
#
|
|
|
|
# Configuration options:
|
2010-06-15 13:47:06 -04:00
|
|
|
# * <tt>:message</tt> - A custom error message (default is: "doesn't match
|
|
|
|
# confirmation").
|
|
|
|
# * <tt>:on</tt> - Specifies when this validation is active (default is
|
|
|
|
# <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
|
|
|
|
# * <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
|
2010-08-14 01:13:00 -04:00
|
|
|
# method, proc or string should return or evaluate to a true or false
|
2010-06-15 13:47:06 -04:00
|
|
|
# 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.
|
|
|
|
# <tt>:unless => :skip_validation</tt>, or
|
2010-06-15 13:47:06 -04:00
|
|
|
# <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
|
2008-03-31 20:05:48 -04:00
|
|
|
# method, proc or string should return or evaluate to a true or false value.
|
|
|
|
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
|