讨论数量:
这是我得验证基类,供参考
<?php
namespace App\Admin\Requests;
use App\Exceptions\ParameterException;
use Illuminate\Foundation\Http\FormRequest;
class BaseRequest extends FormRequest
{
/**
* 单个验证失败后停止
* @var bool
*/
protected $stopOnFirstFailure = true;
/**
* Determine if the admin is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}
/**
* 验证完毕后
*
* @param $validator
* @return void
* @throws ParameterException
*/
public function withValidator($validator): void
{
if ($validator->fails()) {
throw new ParameterException($validator->errors()->first());
}
}
}
看下你全局异常处理里面针对表单验证的是否正确
public function render(Throwable $exception)
{
..........
// 表单验证错误
if ($exception instanceof ValidationException) {
return $this->error($exception->validator->errors()->first());
}
}
如果针对表单请求验证类中做了特殊的处理 如不抛出 ValidationException异常 更换成项目中自己的异常的情况下 可以这样处理
protected function failedValidation(Validator $validator)
{
throw new ApiException($validator->errors()->first());
}
推荐文章: