Class: EmailValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- EmailValidator
- Defined in:
- lib/email_validator.rb
Constant Summary collapse
- Pattern =
Regexp.new('\A' + email_address_regexp + '\Z', Regexp::EXTENDED | Regexp::IGNORECASE, 'n').freeze
- Scanner =
Regexp.new( email_address_regexp, Regexp::EXTENDED | Regexp::IGNORECASE, 'n').freeze
- Separator =
for multiple e-mail addresses
/[;,\s]\s*/.freeze
- Defaults =
{ :message => I18n.t(:invalid_email_address, :scope => [:activerecord, :errors, :messages], :default => 'does not appear to be a valid e-mail address'), :multiple_message => I18n.t(:invalid_multiple_email, :scope => [:activerecord, :errors, :messages], :default => 'appears to contain an invalid e-mail address'), :mx_message => I18n.t(:unroutable_email_address, :scope => [:activerecord, :errors, :messages], :default => 'is not routable'), :check_mx => false, :with => Pattern, :local_length => 64, :domain_length => 255 }.freeze
Class Method Summary collapse
- .errors_on(email, options) ⇒ Object
- .extract(string) ⇒ Object
- .valid?(email, options = {}) ⇒ Boolean
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
Validates whether the specified value is a valid email address, and uses record.errors.add() to add the error if the provided value is not valid.
Class Method Details
.errors_on(email, options) ⇒ Object
68 69 70 71 |
# File 'lib/email_validator.rb', line 68 def errors_on(email, ) = Defaults.merge() [:multiple] ? validate_many(email, ) : validate_one(email, ) end |
.extract(string) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/email_validator.rb', line 57 def extract(string) if string.respond_to?(:encode) string = string.encode('ascii', :undef => :replace) end string.scan(Scanner) end |
.valid?(email, options = {}) ⇒ Boolean
64 65 66 |
# File 'lib/email_validator.rb', line 64 def valid?(email, = {}) errors_on(email, ).nil? end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
Validates whether the specified value is a valid email address, and uses record.errors.add() to add the error if the provided value is not valid.
Configuration options:
-
message
- A custom error message(default: "does not appear to be a valid e-mail address")
-
check_mx
- Check for MX records(default: false)
-
mx_message
- A custom error message when an MX record validation fails(default: "is not routable.")
-
with
- The regex to use for validating the format of the email address(default: +Pattern+)</tt>
-
multiple
- Allow multiple e-mail addresses, separated bySeparator
(default: false)
-
multiple_message
- A custom error message shown when there are 2 or more addressesto validate and one or more is invalid (default: "appears to contain an invalid e-mail address)
-
local_length
- Maximum number of characters allowed in the local part(default: 64)
-
domain_length
- Maximum number of characters allowed in the domain part(default: 255)
50 51 52 53 54 |
# File 'lib/email_validator.rb', line 50 def validate_each(record, attribute, value) return if value.blank? # Use :presence => true error = self.class.errors_on(value, self.) record.errors.add(attribute, :invalid, :message => error, :value => value) if error end |