Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, validates email exclusion

I'm currently trying to validates an email attributes with few things :

  1. Its presence
  2. Its format with a regex
  3. Its uniqueness
  4. Its non presence in a list of mail providers

I'm stuck at the fourth step, I don't really know how to implement it, the main of this steps is to exclude throwable mail providers.

I've currently this :

  validates :email, :presence   => true,
                    :format     => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false },
                    :exclude => Not working when I put a regex here

My problem isn't the regex, it's how to exclude email matching with exclude regex.

Can you help me do this ?

Cordially, Rob.

like image 817
user2462805 Avatar asked Dec 11 '22 12:12

user2462805


1 Answers

If you use devise for user authentication you can uncomment code in devise.rb

  # Email regex used to validate email formats. It simply asserts that
  # one (and only one) @ exists in the given string. This is mainly
  # to give user feedback and not to assert the e-mail validity.
  # config.email_regexp = /\A[^@]+@[^@]+\z/

otherwise i think you can write like

in model

  validates :email, uniqueness: true
  validate  :email_regex

 def email_regex
    if email.present? and not email.match(/\A[^@]+@[^@]+\z/)
      errors.add :email, "This is not a valid email format"
    end
  end
like image 75
Htoo Myat Aung Avatar answered Dec 17 '22 23:12

Htoo Myat Aung