laravel 验证器 Validator::make只显示第一个错误

laravel 验证器 Validator::make只显示第一个错误

以下为方法

```

public function index(Request $request)
{
if ($request->isMethod('post')){

$validator = Validator::make($request->all(), [
'username'=>'required|min:4|max:16',
'password'=>'required!min:6|max:20',
//..........
]);

if($validator->fails()){
return response()->json(['code'=>'201','msg'=>$validator->errors()->first()]);
}

}else{
return view('home.login.reg');
}
}
```

以下为try catch

```
$params = $request->all();
        try{
            $validator = Validator::make($params, [
                'phone' => 'required',
                'code' => 'required',
            ]);
            if ($validator->fails()) {
                $error = $validator->errors()->first();
                throw new \Exception($error);
            }
        }catch (\Exception $e){
            return $this->jsonError($e->getMessage());
        }
```

    A+
发布日期:2022年09月06日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: