I followed the wiki to add a custom email validator with devise. It works but the errors are printed twice once for each validation like below. How to fix this?

Update: The answer linked in the comment does not work. It probably works only if all validations are done in one validates call. In my case one validation is done by devise and other is added by me. To be clear, the model looks like below:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :email, :name
before_save do |user|
user.email = email.downcase
end
validates :name, presence: true, length: { maximum: 50 }
validates :email, email: true, presence: true, reduce: true # This causes 'Email is Invalid' to be printed twice
end
If you just need to change the regular expression that devise is using for email validation, you can do it in config/initializers/devise.rb:
config.email_regexp = /\A[^@]+@[^@]+\z/
Then you won't need to add additional validation to the email field.
You can use email validation with Devise email_regexp, just change in config/initializers/devise.rb
FROM (because it validates some incorrect emails as valid, like [email protected]):
config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
TO:
config.email_regexp = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
I found this solution here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With