Sha256: 8b279b35f0f008fc87b2868a2026b8a93ee923680e0c53db0ea4767ccf0f302d
Contents?: true
Size: 1.24 KB
Versions: 3
Compression:
Stored size: 1.24 KB
Contents
# Allows to check if the value of a specific attribute is a valid email address. # # @example Validate that the user email is a valid email address. # class User << ActiveRecord::Base # attr_accessor :email, :name # validates :email, email: true # end class EmailValidator < ActiveModel::EachValidator # Checks if an attribute value is a valid email address. # # @param [Object] record object to validate # @param [String] attribute name of the object attribute to validate # @param [Object] value attribute value def validate_each(record, attribute, value) allow_blank = options[:allow_blank] || false return if allow_blank && value.blank? unless valid?(value, options) record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email')) end end def self.validate_format(email) !!(email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i) end def self.validate_domain(email, domains) email_downcased = email.to_s.downcase domains.empty? || domains.any? { |domain| email_downcased.end_with?(".#{domain.downcase}") } end private def valid?(email, options) self.class.validate_format(email) \ && self.class.validate_domain(email, Array.wrap(options[:domain])) end end
Version data entries
3 entries across 3 versions & 1 rubygems