laravel 5,更新用户密码 [英] laravel 5,update User password

查看:93
本文介绍了laravel 5,更新用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5开发一个应用程序,该应用程序允许每个用户更新其个人资料.
为了更新密码,用户需要首先输入他的旧密码,如果旧密码匹配,则他的新输入密码将被散列并存储在DB中.如何使用 laravel表单请求验证对此进行验证?

I'm using laravel 5 to develop an app that allow every user to update his profile.
in order to update password, the user needs to first enter his old password and if the old password matched then his newly entered password will be hashed and stored in DB. how can I validate this, using laravel form request validation?

推荐答案

我创建了一个自定义验证器,并将其添加到AppServiceProvider中,如下所示:

I created a custom validator and added it to AppServiceProvider like this:

<?php

namespace App\Providers;

use Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Hash ;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('password_hash_check', function($attribute, $value, $parameters, $validator) {
            return Hash::check($value , $parameters[0]) ;
        });
    }

然后我在表单请求验证器中使用它,如下所示:

then I used it in my form request validator like this:

<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UpdateUserProfileRequest extends Request
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $hashed_password = $this->user()->password ;
        return [
            'oldPassword'=> "password_hash_check:$hashed_password|string|min:6",
            'newPassword' => 'required_with:oldPassword|confirmed|min:6',
        ];
    }

这篇关于laravel 5,更新用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆