DemoIndexRequest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Request\Api\v1;
  4. use Hyperf\Validation\Request\FormRequest;
  5. class DemoIndexRequest extends FormRequest
  6. {
  7. /**
  8. * Determine if the user is authorized to make this request.
  9. */
  10. public function authorize(): bool
  11. {
  12. return true;
  13. }
  14. /**
  15. * Get the validation rules that apply to the request.
  16. */
  17. public function rules(): array
  18. {
  19. return [
  20. 'page' => 'nullable|integer',
  21. 'size' => 'nullable|integer',
  22. 'push_cid' => 'nullable|string',
  23. 'platform' => 'nullable|string',
  24. 'ring_name' => 'nullable|string',
  25. 'order_no' => 'nullable|string',
  26. ];
  27. }
  28. /**
  29. * 获取已定义验证规则的错误消息
  30. */
  31. public function messages(): array
  32. {
  33. return [
  34. 'page.required' => '页码不能为空',
  35. ];
  36. }
  37. /**
  38. * 验证的各字段的含义
  39. * @return array|string[]
  40. */
  41. public function attributes(): array
  42. {
  43. return [
  44. 'page' => '页码',
  45. ];
  46. }
  47. /**
  48. * 表单请求后钩子
  49. * @param $validator
  50. */
  51. public function withValidator($validator)
  52. {
  53. $validator->after(function ($validator) {
  54. //获取参数
  55. $params = $this->validationData();
  56. if (isset($params['page']) && $params['page'] <= 0) {
  57. return $validator->errors()->add('page', 'page 数据有误');
  58. }
  59. if (isset($params['size']) && $params['page'] <= 0) {
  60. return $validator->errors()->add('size', 'size 数据有误');
  61. }
  62. });
  63. }
  64. }