laravel实现密码修改和自定义验证规则及消息

路由:

创建一个Request来验证表单

1 php artisan make:request AdminPost

AdminPost代码:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Auth;
use Validator;
use Hash;
class AdminPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::guard('admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $this->newValidator();
        return [
            'oldPassword'=>'sometimes|required|check_pwd',
            'password'=>'sometimes|required|confirmed',
            'password_confirmation'=>'sometimes|required',
        ];
    }
    public function messages()
    {
        return [
            'password.required'=>'密码不能为空',
            'oldPassword.required'=>'密码不能为空',
            'password_confirmation.required'=>'密码不能为空',
            'password.confirmed'=>'两次密码不一致',
            'oldPassword.check_pwd'=>'原始密码输入有误',
        ];
    }
    public  function newValidator(){
        Validator::extend('check_pwd', function($attribute, $value, $parameters, $validator){
            return Hash::check($value, Auth::guard('admin')->user()->password);

        });

    }
}

控制器处理代码

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AdminPost;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Admin;
use Auth;
public function edit(){
        return view('admin.login.editinfo');
    }
    public function editAction(AdminPost $request){
        $model=Auth::guard('admin')->user();
        $model->password=bcrypt($request['password']);
        $model->save();
        if ($model->save()){
            Auth::guard('admin')->logout();
            return redirect('admin/index');
        }
    }

其中有几个关键点:

1.输入新密码和重复密码怎么样确认是否一样

首先name名一定要取对一定要用下面格式

验证规则这么写

2.怎么样确认输入的原始密码是否正确

在AdminPost.php中添加代码

 

posted @ 2019-07-12 09:49  F4ntasy  阅读(1277)  评论(0编辑  收藏  举报

對不起,您的瀏覽器不支持Flash


請使用支持Flash的瀏覽器訪問本頁,謝謝!