SendRequest.php 1.5 KB

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