aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-09 13:37:11 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-09 13:37:11 +0000
commita65f791fbd8cf16d17a963f32c7b85a5659c04e0 (patch)
treec447bfa77c8e02cfe5d3a57b6ba79a49b66abd17 /activerecord/lib
parentfc817eff447a268ce245a48369e7bd90d8f7f31f (diff)
downloadrails-a65f791fbd8cf16d17a963f32c7b85a5659c04e0.tar.gz
rails-a65f791fbd8cf16d17a963f32c7b85a5659c04e0.tar.bz2
rails-a65f791fbd8cf16d17a963f32c7b85a5659c04e0.zip
Added Base.validate_confirmation that encapsulates the pattern of wanting to validate a password or email address field with a confirmation.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@95 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/validations.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 14e79cf68b..25c43386fd 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -51,6 +51,37 @@ module ActiveRecord
VALIDATIONS.each { |vd| base.class_eval("def self.#{vd}(*methods) write_inheritable_array(\"#{vd}\", methods - (read_inheritable_attribute(\"#{vd}\") || [])) end") }
end
+
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ # Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
+ #
+ # Model:
+ # class Person < ActiveRecord::Base
+ # validate_confirmation :password
+ # end
+ #
+ # View:
+ # <%= password_field "person", "password" %>
+ # <%= password_field "person", "password_confirmation" %>
+ #
+ # The person has to already have a password attribute (a column in the people table), but the password_confirmation is virtual.
+ # It exists only as an in-memory variable for validating the password.
+ #
+ # NOTE: This validation is only happening on create. When you want to update the record, you'll have to decide and pursue your
+ # own course of action.
+ def validate_confirmation(*attr_names)
+ for attr_name in attr_names
+ attr_accessor "#{attr_name}_confirmation"
+ class_eval <<-EOC
+ validate_on_create(Proc.new { |record|
+ record.errors.add("#{attr_name}", "doesn't match confirmation") unless record.#{attr_name} == record.#{attr_name}_confirmation
+ })
+EOC
+ end
+ end
end
# The validation process on save can be skipped by passing false. The regular Base#save method is