Sha256: 4c0db789c4c263a69c577f2ece3f9443259e35b3f371aeb141b26da5c09dbb1b

Contents?: true

Size: 1.01 KB

Versions: 14

Compression:

Stored size: 1.01 KB

Contents

# Borrowed from http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
# Mentioned in tweet here: https://twitter.com/_sohara/status/177120126083141633
require 'mail'
class EmailValidator < ActiveModel::EachValidator
  def validate_each(record,attribute,value)
    unless valid?(value)
      record.errors.add(attribute, :invalid, {:value => value}.merge!(options))
    end
  end

  def valid?(email)
    begin
      m = Mail::Address.new(email)
      # We must check that value contains a domain and that value is an email address
      r = m.domain && m.address == email
      t = m.__send__(:tree)
      # We need to dig into treetop
      # A valid domain must have dot_atom_text elements size > 1
      # user@localhost is excluded
      # treetop must respond to domain
      # We exclude valid email values like <user@localhost.com>
      # Hence we use m.__send__(tree).domain
      r &&= (t.domain.dot_atom_text.elements.size > 1)
    rescue Exception => e
      r = false
    end
    r
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-2.3.2 lib/spree/core/validators/email.rb
spree_core-2.2.5 lib/spree/core/validators/email.rb
spree_core-2.1.8 lib/spree/core/validators/email.rb
spree_core-2.3.1 lib/spree/core/validators/email.rb
spree_core-2.2.4 lib/spree/core/validators/email.rb
spree_core-2.2.3 lib/spree/core/validators/email.rb
spree_core-2.3.0 lib/spree/core/validators/email.rb
spree_core-2.2.2 lib/spree/core/validators/email.rb
spree_core-2.1.7 lib/spree/core/validators/email.rb
spree_core-2.1.6 lib/spree/core/validators/email.rb
spree_core-2.2.1 lib/spree/core/validators/email.rb
spree_core-2.2.0 lib/spree/core/validators/email.rb
spree_core-2.1.5 lib/spree/core/validators/email.rb
spree_core-2.1.4 lib/spree/core/validators/email.rb