Reputation: 457
In Devise Validatable module contains
validates_uniqueness_of :email, :allow_blank => true, :if => :email_changed?
How to disable this validator?
Upvotes: 16
Views: 10974
Reputation: 43
Followed this and then,
If you already ran device migrations, also make sure to remove that the validation fields like
encrypted_password
which are set as can not be null (Added by device) from the database schema.
Run a migration with something like...
change_column :users, :email, :string, null: true
change_column :users, :encrypted_password, :string, null: true
Upvotes: 0
Reputation: 5708
In the model (e.g. User) that you set up devise with:
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
Remember to run the migration to drop the index on email:
def change
sql = 'DROP INDEX index_users_on_email ON users'
ActiveRecord::Base.connection.execute(sql)
end
Note: Devise validates email even though authentication_keys
has been set to something else.
Upvotes: 2
Reputation: 913
Depending on your needs you may not need to completely tear out the Validatable
module. If you just want to allow empty strings in the email column then you can override the email_required?
method...
class User
...
def email_required?
false
end
Upvotes: 4
Reputation: 3186
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
Upvotes: 23
Reputation: 1035
I'm afraid that you'll find it very ugly but:
In 1.x devise(we are talking about branch 1.x, right?) there is no options to choose it, so the only way remains is to remove unnecessary validator in class definition after devise directives:
class User extends Mongoid document(no mater, AR is the same here, just place AR Validator class in delete_if method below) and contains devise initializers, so this hack seems to works:
User._validators[:email].try{ |validators|
validators.delete_if{ |validator|
validator.is_a? Mongoid::Validations::UniquenessValidator
}
}
So, User class definition could be:
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
Upvotes: 3