gpt4 book ai didi

ruby-on-rails - 在设计中跳过电子邮件验证

转载 作者:行者123 更新时间:2023-12-04 02:56:42 24 4
gpt4 key购买 nike

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

最佳答案

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

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模块并推出您自己的验证。
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

关于ruby-on-rails - 在设计中跳过电子邮件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9210844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com