demarches-normaliennes/app/validators/email_format_validator.rb
2017-06-13 10:35:32 +02:00

10 lines
278 B
Ruby

class EmailFormatValidator < ActiveModel::Validator
def email_regex
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
end
def validate(record)
return if record.email.blank?
record.errors[:base] << "Email invalide" unless email_regex.match(record.email)
end
end