confirmation.rb 2.7 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
        klass.send(:attr_accessor, *attributes.map { |attribute| :"#{attribute}_confirmation" })
14
      end
15 16
    end

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