Validate email address properly
This commit is contained in:
parent
e44535daee
commit
2a2b0a90dc
15
app/models/concerns/email_validatable.rb
Normal file
15
app/models/concerns/email_validatable.rb
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
require 'mail'
|
||||||
|
|
||||||
|
module EmailValidatable
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
class EmailValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(record, attribute, value)
|
||||||
|
begin
|
||||||
|
a = Mail::Address.new(value)
|
||||||
|
rescue Mail::Field::ParseError
|
||||||
|
record.errors[attribute] << (options[:message] || "is not a valid address")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,10 +1,13 @@
|
|||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
|
include EmailValidatable
|
||||||
|
|
||||||
# Relations
|
# Relations
|
||||||
has_many :invitations, dependent: :destroy
|
has_many :invitations, dependent: :destroy
|
||||||
|
|
||||||
validates_uniqueness_of :cn
|
validates_uniqueness_of :cn
|
||||||
validates_uniqueness_of :email
|
|
||||||
validates_length_of :cn, :minimum => 3
|
validates_length_of :cn, :minimum => 3
|
||||||
|
validates_uniqueness_of :email
|
||||||
|
validates :email, email: true
|
||||||
|
|
||||||
# Include default devise modules. Others available are:
|
# Include default devise modules. Others available are:
|
||||||
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
||||||
|
Loading…
x
Reference in New Issue
Block a user