| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- declare(strict_types=1);
- namespace App\Request;
- /**
- *
- */
- use App\Constants\ErrorCode;
- use App\Exception\ApiException;
- use Hyperf\Contract\ValidatorInterface;
- use Hyperf\Validation\Request\FormRequest;
- class UploadRequest extends FormRequest
- {
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- */
- public function rules(): array
- {
- return [
- 'type' => 'nullable|string',
- 'file' => 'required|file',
- ];
- }
- public function messages(): array
- {
- return [
- 'required' => ':attribute 必填',
- 'file' => ':attribute 必须上传',
- ];
- }
- // 这里我是希望走到我自己定义的 异常处理,你如果没有自定义,去掉这个函数的重写即可。
- protected function failedValidation(ValidatorInterface $validator)
- {
- throw new ApiException(ErrorCode::SERVER_ERROR, $validator->errors()->first());
- }
- }
|