123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- declare(strict_types=1);
- namespace App\Request\Api\v1\Sms;
- use Hyperf\Validation\Request\FormRequest;
- class SendRequest 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 [
- 'mobile' => 'required|string',
- 'event' => 'required|string'
- ];
- }
- /**
- * 获取已定义验证规则的错误消息
- */
- public function messages(): array
- {
- return [
- 'mobile.required' => '手机号不能为空',
- 'event.required' => '事件不能为空',
- ];
- }
- /**
- * 验证的各字段的含义
- * @return array|string[]
- */
- public function attributes(): array
- {
- return [
- 'mobile' => '手机号',
- 'event' => '事件',
- ];
- }
- /**
- * 表单请求后钩子
- * @param $validator
- */
- public function withValidator($validator)
- {
- $validator->after(function ($validator) {
- //获取参数
- $params = $this->validationData();
- if (isset($params['mobile']) && !preg_match("/^1[3-9]\d{9}$/", $params['mobile'])) {
- return $validator->errors()->add('mobile', '手机号格式不正确');
- }
- });
- }
- }
|