Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/valid_email/email_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/valid_email/email_validator.rb', line 5

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

  r = ValidateEmail.valid?(value)
  # Check if domain has DNS MX record
  if r && options[:mx]
    require 'valid_email/mx_validator'
    r = MxValidator.new(:attributes => attributes, message: options[:message]).validate(record)
  elsif r && options[:mx_with_fallback]
    require 'valid_email/mx_with_fallback_validator'
    r = MxWithFallbackValidator.new(:attributes => attributes, message: options[:message]).validate(record)
  end
  # Check if domain is disposable
  if r && options[:ban_disposable_email]
    require 'valid_email/ban_disposable_email_validator'
    r = BanDisposableEmailValidator.new(:attributes => attributes, message: options[:message]).validate(record)
  end
  unless r
    msg = (options[:message] || I18n.t(:invalid, :scope => "valid_email.validations.email"))
    record.errors.add attribute, (msg % {value: value})
  end
end