ruby-on-railsruby-on-rails-3devisereset-password

Validation errors are triggered when I'm trying to reset password


I'm using devise for authentication and I've added some other fields to the users table after setting up devise. User can sign-up by entering email and password only and after sign-up user can edit his profile. For that I've used :on => update. But now when I'm trying to reset the password validations are triggering error like name cannot be blank and blah blah. I'm using devise and using registrations#edit for resetting password. Below is my user model.

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :dob, :phone, :address, :state, :city, :country, :photo

  has_attached_file :photo, :styles => { :small => "150x150>", :medium => "300x300>", :large => "500x500>" }
  has_many :blogs, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  has_many :followers, :through => :blogs
  has_many :followings, :class_name => 'Follower', :foreign_key => 'user_id'
  has_many :following_blogs, :through => :followings, :source => :blog
  has_many :blog_followers, :through => :followers, :source => :user

  phone_regex = /^[0-9]+$/

  validates_attachment_size :photo, :less_than => 3.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpeg','image/png','image/jpg','image/gif']

  validates :name, :presence => true, :on => :update
  validates :dob, :presence => true, :on => :update
  validates :phone, :format => { :with => phone_regex }, :length => { :is => 10 }, :on => :update
  validates :address, :presence => true, :on => :update
  validates :state, :presence => true,:length => { :maximum => 30 }, :on => :update
  validates :city, :presence => true, :length => { :maximum => 30 }, :on => :update
  validates :country, :presence => true, :length => { :maximum => 30 }, :on => :update
end

Solution

  • Actually, the issue is you are using validatable devise module. It puts the automatic validations on email & password even on updates. The better option would be to add your own validation on email & password on create and make these changes in the model or remove it entirely.

    User Model
    
    def email_required?
      false
    end
    
    def password_changed?
      false
    end
    

    Hope that will help.