在设计中跳过电子邮件验证 [英] Skip email validation in Devise

查看:37
本文介绍了在设计中跳过电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

In Devise Validatable module contains validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed? How to disable this validator?

解决方案

Per 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.

Bolding my emphasis.

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

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

Then make whatever changes are necessary.

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

这篇关于在设计中跳过电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆