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").
|
2012-07-10 03:07:38 -04:00
|
|
|
#
|
|
|
|
# There is also a list of default options supported by every validator:
|
|
|
|
# +:if+, +:unless+, +:on+ and +:strict+.
|
|
|
|
# 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
|