Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/kangal/email.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kangal/email.rb', line 7

def validate_each(object, attribute, value)

  return if options[:allow_nil] && value.nil?
  return if options[:allow_blank] && value.blank?

  begin
    m = Mail::Address.new(value)
    r = m.domain && m.address == value
    t = m.__send__(:tree)
    r &&= (t.domain.dot_atom_text.elements.size > 1)
  rescue Exception => e
    r = false
  end
  object.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.email')) unless r
end