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


    路由:

    创建一个Request来验证表单

    1 php artisan make:request AdminPost

    AdminPost代码:

    <?php
    
    namespace AppHttpRequests;
    
    use IlluminateFoundationHttpFormRequest;
    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 AppHttpControllersAdmin;
    
    use AppHttpRequestsAdminPost;
    use IlluminateHttpRequest;
    use AppHttpControllersController;
    use AppModelAdmin;
    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中添加代码

  • 相关阅读:
    hadoop基础
    数据库基础知识
    sqoop基础
    大数据之常用linux常用命令
    zooKeeper基础
    impala基础
    Hbase基础
    Commitlint 提交规范类型
    理解JS闭包
    JS函数作用域及作用域链理解
  • 原文地址:https://www.cnblogs.com/F4natasy/p/11174260.html
Copyright © 2020-2023  润新知