Module: ActiveRecord::Validations::ClassMethods
- Defined in:
- lib/validates_email_format_of.rb
Instance Method Summary collapse
-
#validates_email_format_of(*attr_names) ⇒ Object
Validates whether the value of the specified attribute is a valid email address.
Instance Method Details
#validates_email_format_of(*attr_names) ⇒ Object
Validates whether the value of the specified attribute is a valid email address
class User < ActiveRecord::Base
validates_email_format_of :email, :on => :create
end
Configuration options:
-
message- A custom error message (default is: “ does not appear to be a valid e-mail address”) -
on- Specifies when this validation is active (default is :save, other options :create, :update) -
allow_nil- Allow nil values (default is false) -
allow_blank- Allow blank values (default is false) -
check_mx- Check for MX records (default is false) -
mx_message- A custom error message when an MX record validation fails (default is: “ is not routable.”) -
if- Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value. -
unless- See:if
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/validates_email_format_of.rb', line 72 def validates_email_format_of(*attr_names) = { :on => :save, :allow_nil => false, :allow_blank => false } .update(attr_names.pop) if attr_names.last.is_a?(Hash) validates_each(attr_names, ) do |record, attr_name, value| v = value.to_s errors = ValidatesEmailFormatOf::validate_email_format(v, ) errors.each do |error| record.errors.add(attr_name, error) end unless errors.nil? end end |