Validate email address properly
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user