• 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中添加代码

  • 相关阅读:
    VS一个奇怪的发布问题
    VS删除空白行
    IDEA Rider 准备试用一段时间(1)
    IDEA Rider使用64位IISExpress(3)
    IDEA rider 管道模式 经典模式(2)
    VSCode批量替换使用注意问题
    IScroll Unable to preventDefault inside passive event listener due to target being treated as passive
    数据库索引也不是多多益善
    css设置手型光标
    javascript参数化拼接字符串两种方法
  • 原文地址:https://www.cnblogs.com/F4natasy/p/11174260.html
Copyright © 2020-2023  润新知