0

Possible Duplicate:
Skip email validation for omniauth-twitter on devise 2.0

I am using this validation rules for email:

  validates :email,
            :presence => true, 
            :format => { :with => VALID_EMAIL_REGEX }, 
            :uniqueness => {:case_sensitive => false }

  before_save do |user| 
        user.email = email.downcase#
        user.slug  = user.username.parameterize
  end

And I tried to add:

  def email_required?
    super && provider.blank?
  end

For skipping email validation, if a visitor want to sign up through Twitter, but I am still getting error here, it looks that the email_required? doesn't work.

What am I missing?

Thank you

Community
  • 1
  • 1
user984621
  • 46,344
  • 73
  • 224
  • 412

1 Answers1

1

email_required DOES work if you want to skip devise's validation, see: https://github.com/plataformatec/devise/blob/c179cef365f7188c91cbbc3db924a9f1f9563c3c/lib/devise/models/validatable.rb#L29 But if you want to skip your own email validation you can use something like this:

validates :email,
          :presence => true, 
          :format => { :with => VALID_EMAIL_REGEX }, 
          :uniqueness => {:case_sensitive => false },
          :if => 'provider.blank?'
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
  • Thanks for your reply nash. When I try this, there is still one issue - I am getting the error `undefined method 'downcase' for nil:NilClass` on the first line in the `before_save` block. How can I handle this issue? In the database scheme is set up that email cannot be blank. – user984621 Jan 27 '13 at 18:12
  • you can just checking if there is an email in `before_save`: `user.email = email.downcase if email` – Vasiliy Ermolovich Jan 27 '13 at 19:14
  • 1
    But in that case I get the error message `PG::Error: ERROR: null value in column "email" violates not-null constraint` – user984621 Jan 27 '13 at 21:25