MessageRequest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Request\Api\v1\Common;
  4. use Hyperf\Validation\Request\FormRequest;
  5. class MessageRequest 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' => 'required|integer:strict',
  21. 'list_rows' => 'required|integer:strict',
  22. ];
  23. }
  24. /**
  25. * 获取已定义验证规则的错误消息
  26. */
  27. public function messages(): array
  28. {
  29. return [
  30. 'page.required' => '页码不能为空',
  31. ];
  32. }
  33. /**
  34. * 验证的各字段的含义
  35. * @return array|string[]
  36. */
  37. public function attributes(): array
  38. {
  39. return [
  40. 'page' => '页码',
  41. ];
  42. }
  43. /**
  44. * 表单请求后钩子
  45. * @param $validator
  46. */
  47. public function withValidator($validator)
  48. {
  49. $validator->after(function ($validator) {
  50. //获取参数
  51. $params = $this->validationData();
  52. if (isset($params['page']) && $params['page'] <= 0) {
  53. return $validator->errors()->add('page', 'page 数据有误');
  54. }
  55. if (isset($params['list_rows']) && $params['page'] <= 0) {
  56. return $validator->errors()->add('list_rows', 'list_rows 数据有误');
  57. }
  58. });
  59. }
  60. }