Adds validations for email addresses
Methods
Classes and Modules
Module PluginAWeek::ValidatesAsEmailAddress::RFC1035Module PluginAWeek::ValidatesAsEmailAddress::RFC822
Constants
| EmailAddress | = | begin local_part = RFC822::LocalPart.source |
| Matches email addresses with domains that follow the RFC 1035 standard | ||
| EmailAddress | = | begin local_part = LocalPart.source |
| Matches email addresses according to the RFC822 standard | ||
Public Instance methods
Validates whether the value of the specific attribute matches against the RFC822/RFC1035 specification.
class Person < ActiveRecord::Base
validates_as_email_address :email, :on => :create
end
This will also validate that the email address is within the specification limits, i.e. between 3 and 320 characters in length.
Configuration options for length:
- minimum - The minimum size of the attribute
- maximum - The maximum size of the attribute
- is - The exact size of the attribute
- within - A range specifying the minimum and maximum size of the attribute
- in - A synonym(or alias) for :within
- too_long - The error message if the attribute goes over the maximum (default is: "is too long (maximum is %d characters)")
- too_short - The error message if the attribute goes under the minimum (default is: "is too short (minimum is %d characters)")
- wrong_length - The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
Configuration options for format:
- wrong_format - A custom error message (default is: "is an invalid email address")
Miscellaneous configuration options:
- allow_nil - Attribute may be nil; skip validation.
- on - Specifies when this validation is active (default is :save, other options :create, :update)
- if - Specifies a method, proc or string to call to determine if the validation should
occur (e.g. :if => :allow_validation, or :if => lambda { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.
- strict - Specifies if the domain part of the email should be compliant to RFC 1035 (default is true). If set to false domains such as ’-online.com’, ’[127.0.0.1]’ become valid.
[ show source ]
# File lib/validates_as_email_address.rb, line 37
37: def validates_as_email_address(*attr_names)
38: configuration = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
39: configuration.reverse_merge!(
40: :wrong_format => ActiveRecord::Errors.default_error_messages[:invalid_email],
41: :strict => true
42: )
43:
44: # Add format validation
45: format_configuration = configuration.dup
46: format_configuration[:message] = configuration.delete(:wrong_format)
47: format_configuration[:with] = configuration[:strict] ? RFC1035::EmailAddress : RFC822::EmailAddress
48: validates_format_of attr_names, format_configuration
49:
50: # Add length validation
51: length_configuration = configuration.dup
52: length_configuration.reverse_merge!(:within => 3..320) unless ([:minimum, :maximum, :is, :within, :in] & configuration.keys).any?
53: validates_length_of attr_names, length_configuration
54: end