Skip validation for some members in Devise model during password reset

前端 未结 4 1203
Happy的楠姐
Happy的楠姐 2021-01-18 12:09

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,

4条回答
  •  情深已故
    2021-01-18 12:36

    In the Devise model you can override reset_password! and use your own validations. For example:

    def reset_password!(new_password, new_password_confirmation)
      self.password = new_password
      self.password_confirmation = new_password_confirmation
    
      validates_presence_of     :password
      validates_confirmation_of :password
      validates_length_of       :password, within: Devise.password_length, allow_blank: true
    
      if errors.empty?
        clear_reset_password_token
        after_password_reset
        save(validate: false)
      end
    end
    

提交回复
热议问题