Jake
Jake

Reputation: 31

Rails Password Validate only in certain cases

In my rails 3 application, I have a user sign up form. I have it require and validate the password for signing up, however, if a user connects using omniauth, I cannot retrieve their password which brings me to my question. How can I make it so the password field does not get validated in certain cases?

I am aware that I can do user.save(:validate => false), but that takes off all the validations.

P.S. i watched railscasts episode 235 & 236, however Ryan Bates does it with Devise, and I am doing it with my own login system.

Upvotes: 1

Views: 537

Answers (2)

Keith Woody
Keith Woody

Reputation: 11

you can add conditions to the validation in the model:

validates_presence_of :password, :unless => :account_from_omniauth?

assuming you also define the method

def account_from_omniauth?
  true if //...your conditions
  false
end

Upvotes: 1

Mirko
Mirko

Reputation: 5286

You can write your own validation for password with a check if the user is using oauth.
Check this method: validate(*args, &block)

Upvotes: 1

Related Questions