Additional configuration for validatable

Added the ability to customize password length (via Devise.password_length) and the regular expression used for validating email (via Devise.email_regex)
This commit is contained in:
Jacques Crocker
2010-03-26 13:52:12 -07:00
parent e127463ac8
commit fd035b841b
3 changed files with 18 additions and 6 deletions

View File

@@ -43,9 +43,6 @@ module Devise
:bcrypt => 60
}
# Email regex used to validate email formats. Adapted from authlogic.
EMAIL_REGEX = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
# Used to encrypt password. Please generate one with rake secret.
mattr_accessor :pepper
@@pepper = nil
@@ -57,7 +54,15 @@ module Devise
# Keys used when authenticating an user.
mattr_accessor :authentication_keys
@@authentication_keys = [ :email ]
# Range validation for password length
mattr_accessor :password_length
@@password_length = 6..20
# Email regex used to validate email formats. Adapted from authlogic.
mattr_accessor :email_regex
@@email_regex = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
# Time interval where the remember me token is valid.
mattr_accessor :remember_for
@@remember_for = 2.weeks

View File

@@ -16,12 +16,12 @@ module Devise
base.class_eval do
validates_presence_of :email
validates_uniqueness_of :email, :scope => authentication_keys[1..-1], :allow_blank => true
validates_format_of :email, :with => EMAIL_REGEX, :allow_blank => true
validates_format_of :email, :with => Devise.email_regex, :allow_blank => true
with_options :if => :password_required? do |v|
v.validates_presence_of :password
v.validates_confirmation_of :password
v.validates_length_of :password, :within => 6..20, :allow_blank => true
v.validates_length_of :password, :within => Devise.password_length, :allow_blank => true
end
end
end

View File

@@ -38,6 +38,13 @@ Devise.setup do |config|
# The time the user will be remembered without asking for credentials again.
# config.remember_for = 2.weeks
# ==> Configuration for :validatable
# Range for password length
# config.password_length = 6..20
# Regex to use to validate the email address
# config.email_regex = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
# ==> Configuration for :timeoutable
# The time you want to timeout the user session without activity. After this
# time the user will be asked for credentials again.