Devise: Skip validation for some members in Devise model during password reset

Created on 6 Feb 2012  ·  5Comments  ·  Source: heartcombo/devise

My User (Devise) model also has name, city, nation, phone members.

In the create registration page - I validates_presence_of :city, :nation, :phone, :name, :email, :on => :create
In the edit registration page - I validates_presence_of :city, :nation, :phone, :name, :on => :update

Now when I set a new password on forgot_password_page, it asks for the presence of city, nation, phone, name inside Devise::PasswordsController#update

How can I handle selective validations?

I am guessing it should be something like,

validates_presence_of :city, :nation, :phone, :name, :on => :update, :if => :not_recovering_password

def not_recovering_password
  # what goes here
end

Posted in Stackoverflow

Most helpful comment

I had the same issue today, passed around it by adding if: "!encrypted_password_changed?" on validations I don't want running when changing password. Hope it will help.

All 5 comments

Something is weird in your setup. If the model is created with nation, phone, etc, when resetting the password all those fields should already be there and the validation should not trigger. In any case, it seems the best option for you is to simply override:

https://github.com/plataformatec/devise/blob/master/lib/devise/models/recoverable.rb#L29

So you can set a flag and skip the validations.

Uh, I forgot to mention that the users have the option to use facebook connect. When a user chooses that, and is successfully authenticated by facebook, we create a devise user

User.create(:email => data.email, :name => data.name, :facebook_token => token) #data = user information facebook sends

So this user is created though :city, :nation, :phone are not set.

Now when this user clicks on "forgot password" and follows the password reset link, since the aforementioned fields were never set :on_create, validation for them :on_update fails.

Is this a good approach?

My suggestion is to simply remove those validations. You are going to run into other issues, you will make it hard for users to update their profiles gradually and so forth.

Thank you.

I had the same issue today, passed around it by adding if: "!encrypted_password_changed?" on validations I don't want running when changing password. Hope it will help.

Was this page helpful?
0 / 5 - 0 ratings