Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions lib/validate_email.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
require 'active_model'
require 'mail'

module ActiveModel
module Validations
class EmailValidator < ActiveModel::EachValidator

def initialize(options)
options.reverse_merge!(:message => "is not a valid email address")
super(options)
end

def validate_each(record, attribute, value)
return if options[:allow_nil] && value.nil?

begin
mail = Mail::Address.new(value)

unless mail.address == value && mail.domain.split(".").length > 1
record.errors.add(attribute, options[:message])
record.errors.add(attribute, :invalid_email, :default => options[:message], :value => value)
end
rescue
record.errors.add(attribute, options[:message])
record.errors.add(attribute, :invalid_email, :default => options[:message], :value => value)
end
end
end
Expand Down