| 12345678910111213141516171819202122232425262728 |
- <?php
- namespace App\Traits;
- use Illuminate\Contracts\Validation\Validator;
- use Illuminate\Validation\ValidationException;
- use Symfony\Component\HttpFoundation\Response;
- trait RequestApiFormValidation
- {
- // 重写ajax请求验证错误响应格式(防止验证422报错)
- protected function failedValidation(Validator $validator)
- {
- // 此处自定义表单验证错误信息
- $data = [
- 'code' => 200,
- 'success' => false,
- 'errors' => $validator->errors()
- ];
- $response = new Response(json_encode($data));
- throw (new ValidationException($validator, $response))
- ->errorBag($this->errorBag)
- ->redirectTo($this->getRedirectUrl());
- }
- }
|