logesh
logesh

Reputation: 2662

How to customize devise such that validation is done only at specified condition?

I have devise for authentication and i have not used validatable and i send the request and along to it i send a type. for example

http://localhost:3000/signup/new?type=twitter 

in the above shown i have used type=twitterand in case it is twitter i do not want to validate email i.e., do not need validates_presence_of :email but in case i send type=email then i want to check presence of mail and whether mail has not already taken and check mail specified is valid or not? How can i do this? Please help me.

Upvotes: 1

Views: 307

Answers (2)

Ashitaka
Ashitaka

Reputation: 19203

You could add the following to your controller:

if params[:type] == 'twitter'
  user.save(:validate => false)

However, I wouldn't advise doing this as you would then have invalid data in your database. It would be best if you actually asked the user to enter his email when logging in with Twitter. This whole challenge is tackled by Ryan Bates on this episode about omniauth.

EDIT: I investigated a bit and if you check devise's source code, you'll notice that they now have this method called email_required?.

So all you have to do is override that method. How does you user model look? How are you allowing the user to login using Twitter? If you edit your question and show us that, we might be able to help you a little further.

Upvotes: 2

ksol
ksol

Reputation: 12235

I think @Ashitaka has a good point when he says you should ask for the user's email no matter what.

However, for what you are trying to achieve, I would create my own validator class. You can see how to achieve this in this rails guide.

Upvotes: 0

Related Questions