confirmation.rb 2.9 KB
Newer Older
1
module ActiveModel
2 3

  # == Active Model Confirmation Validator
4
  module Validations
5 6
    class ConfirmationValidator < EachValidator
      def validate_each(record, attribute, value)
7 8 9
        if (confirmed = record.send("#{attribute}_confirmation")) && (value != confirmed)
          record.errors.add(attribute, :confirmation, options)
        end
10
      end
11

12
      def setup(klass)
13 14 15
        klass.send(:attr_accessor, *attributes.map do |attribute|
          :"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation")
        end.compact)
16
      end
17 18
    end

19
    module HelperMethods
20
      # Encapsulates the pattern of wanting to validate a password or email
21
      # address field with a confirmation. For example:
22 23 24 25
      #
      #   Model:
      #     class Person < ActiveRecord::Base
      #       validates_confirmation_of :user_name, :password
26
      #       validates_confirmation_of :email_address,
27
      #                                 :message => "should match confirmation"
28 29 30 31 32 33
      #     end
      #
      #   View:
      #     <%= password_field "person", "password" %>
      #     <%= password_field "person", "password_confirmation" %>
      #
34
      # The added +password_confirmation+ attribute is virtual; it exists only
35
      # as an in-memory attribute for validating the password. To achieve this,
36
      # the validation adds accessors to the model for the confirmation
37
      # attribute.
38 39
      #
      # NOTE: This check is performed only if +password_confirmation+ is not
40 41
      # +nil+, and by default only on save. To require confirmation, make sure
      # to add a presence check for the confirmation attribute:
42 43 44 45
      #
      #   validates_presence_of :password_confirmation, :if => :password_changed?
      #
      # Configuration options:
46 47
      # * <tt>:message</tt> - A custom error message (default is: "doesn't match
      #   confirmation").
X
Xavier Noria 已提交
48 49 50
      # * <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>.
51 52 53
      # * <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
54
      #   method, proc or string should return or evaluate to a true or false
55
      #   value.
56 57 58
      # * <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
59
      #   <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
60 61
      #   method, proc or string should return or evaluate to a true or false value.
      def validates_confirmation_of(*attr_names)
62
        validates_with ConfirmationValidator, _merge_attributes(attr_names)
63 64 65
      end
    end
  end
66
end