UploadRequest.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Request;
  4. /**
  5. *
  6. */
  7. use App\Constants\ErrorCode;
  8. use App\Exception\ApiException;
  9. use Hyperf\Contract\ValidatorInterface;
  10. use Hyperf\Validation\Request\FormRequest;
  11. class UploadRequest extends FormRequest
  12. {
  13. /**
  14. * Determine if the user is authorized to make this request.
  15. */
  16. public function authorize(): bool
  17. {
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. */
  23. public function rules(): array
  24. {
  25. return [
  26. 'type' => 'nullable|string',
  27. 'file' => 'required|file',
  28. ];
  29. }
  30. public function messages(): array
  31. {
  32. return [
  33. 'required' => ':attribute 必填',
  34. 'file' => ':attribute 必须上传',
  35. ];
  36. }
  37. // 这里我是希望走到我自己定义的 异常处理,你如果没有自定义,去掉这个函数的重写即可。
  38. protected function failedValidation(ValidatorInterface $validator)
  39. {
  40. throw new ApiException(ErrorCode::SERVER_ERROR, $validator->errors()->first());
  41. }
  42. }