在Devise中跳过电子邮件验证 [英] Skip email validation in Devise
问题描述
在Devise Validatable模块中包含
validates_uniqueness_of:email,:allow_blank => true,if =>:email_changed?
如何禁用此验证器?
In Devise Validatable module contains
validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?
How to disable this validator?
推荐答案
每 Devise自己的可验证模块的文档 ...
可验证创建用户电子邮件和密码所需的所有验证。 这是可选的,您可能希望自己创建验证。自动验证电子邮件是否存在,唯一且格式有效。还测试密码,确认和长度的存在。
Validatable creates all needed validations for a user email and password. It's optional, given you may want to create the validations by yourself. Automatically validate if the email is present, unique and its format is valid. Also tests presence of password, confirmation and length.
强调我的重点。
您应该禁用 Validatable
模块,并滚动您自己的验证。
You should disable the Validatable
module and roll your own validations.
devise :database_authenticatable, :registerable, :rememberable,
:trackable, :timeoutable, :confirmable, :recoverable, :lockable
# :validatable <-- this one needs to go
查看 lib / devise / models / validatable.rb
并将相关部分拉入您自己的User类。对于当前 3.2.x
版本行,应该看起来像这样...
Review the contents of lib/devise/models/validatable.rb
and pull the relevant sections into your own User class. For the current 3.2.x
version line, it should look something like this...
class User < ActiveRecord::Base
# From Devise module Validatable
validates_presence_of :email, if: :email_required?
validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
validates_format_of :email, with: email_regexp, allow_blank: true, if: :email_changed?
validates_presence_of :password, if: :password_required?
validates_confirmation_of :password, if: :password_required?
validates_length_of :password, within: password_length, allow_blank: true
# [ ... ] The rest of your model stuff
protected
# From Devise module Validatable
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
# From Devise module Validatable
def email_required?
true
end
end
然后make
一个现实世界的场景:我使用 Paranoia宝石上的一些项目,这将不适用于这个模块。所以我删除它,并自定义电子邮件唯一性检查读为...
A real world scenario: I use the Paranoia gem on a number of projects, which won't work with this module. So I remove it and customize the email uniqueness check to read as...
validates_uniqueness_of :email, scope: :deleted_at
这篇关于在Devise中跳过电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!