在Devise中跳过电子邮件验证

时间:2012-02-09 12:28:45

标签: ruby-on-rails devise

在Devise Validatable模块中包含 validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed? 如何禁用此验证器?

5 个答案:

答案 0 :(得分:13)

Devise's own documentation on the Validatable module ...

  

Validatable会为用户电子邮件和密码创建所有必需的验证。 这是可选的,因为您可能希望自己创建验证。自动验证电子邮件是否存在,唯一且其格式是否有效。还测试密码,确认和长度的存在。

强调我的重点。

您应该停用Validatable模块并进行自己的验证。

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版本行,它应该看起来像这样......

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

然后进行必要的更改。

真实场景:我在许多项目中使用Paranoia gem,这对于此模块无效。所以我删除它并自定义电子邮件唯一性检查以读为...

validates_uniqueness_of :email, scope: :deleted_at

答案 1 :(得分:3)

我担心你会发现它非常丑陋但是:

在1.x设计中(我们正在谈论分支1.x,对吗?)没有选择它的选项,所以唯一的方法是在设计指令后删除类定义中不必要的验证器:

类用户扩展Mongoid文档(没有mater,AR在这里是相同的,只是在下面的delete_if方法中放置AR Validator类)并包含设计初始化器,所以这个hack似乎有用:

User._validators[:email].try{ |validators|
  validators.delete_if{ |validator|
    validator.is_a? Mongoid::Validations::UniquenessValidator
  }
}

因此,用户类定义可以是:

class User
  include Mongoid::Document
  devise :database_authenticatable, :registerable, :validatable
  _validators[:email].try{ |validators|
    validators.delete_if{ |validator|
      validator.is_a? Mongoid::Validations::UniquenessValidator
    }
  }
  # ...
end

答案 2 :(得分:1)

根据您的需要,您可能不需要完全撕掉Validatable模块。如果您只想在电子邮件列中允许空字符串,则可以覆盖email_required?方法...

class User
...
def email_required?
  false
end

答案 3 :(得分:0)

在您设置的设计模型(例如用户)中:

class User
  # devise :database_authenticatable, .... :validatable
  def email_required?
    false
  end

  ## for ActiveRecord < 5.1
  def email_changed?
    false
  end

  ## for ActiveRecord 5.1+
  def will_save_change_to_email?
    false
  end

end

记住要运行迁移以删除电子邮件中的索引:

def change
  sql = 'DROP INDEX index_users_on_email ON users'
  ActiveRecord::Base.connection.execute(sql)
end

注意:即使authentication_keys设置为其他设置,Devise也会验证电子邮件。

答案 4 :(得分:0)

跟随this,然后

  

如果您已经进行过设备迁移,还请确保删除设置为的验证字段,例如emailencrypted_password不能为空(由设备添加)来自数据库架构。

使用类似...的方式进行迁移

change_column :users, :email, :string, null: true
change_column :users, :encrypted_password, :string, null: true