Files
usedcar/app/apis/validate/ContactsValidator.php
T

73 lines
2.4 KiB
PHP
Raw Normal View History

2025-05-14 13:19:23 +08:00
<?php
namespace app\apis\validate;
use think\Validate;
class ContactsValidator extends Validate
{
2025-06-05 16:09:24 +08:00
public function __construct()
{
parent::__construct();
// 初始化验证消息
$this->message = [
'name.require' => trans('name_not_null'),
'name.max' => trans('name_max'),
'name.checkEmptyString' => trans('name_check_empty'),
'phone.require' => trans('phone_not_null'),
'phone.checkInternationalPhone' => trans('phone_international'),
'content.require' => trans('content_not_null'),
'content.min' => trans('content_min'),
'content.max' => trans('content_max'),
'vehicle_id.integer' => trans('vehicle_id_integer'),
'email.email' => trans('email'),
];
}
2025-05-14 13:19:23 +08:00
// 验证规则
protected $rule = [
'name' => 'require|max:255|checkEmptyString',
'phone' => 'require|checkInternationalPhone',
'content' => 'require|min:10|max:65535',
'vehicle_id' => 'integer',
'email' => 'email'
];
// 错误提示信息
2025-06-05 16:09:24 +08:00
protected $message = [];
2025-05-14 13:19:23 +08:00
// 验证场景
protected $scene = [
'store' => ['name', 'phone', 'content', 'email', 'vehicle_id']
];
// 自定义验证规则 - 检查国际电话号码
protected function checkInternationalPhone($value)
{
/**
* 国际电话验证规则:
* 1. 必须以 + 开头
* 2. 国家代码1-4位数字
* 3. 后续号码总长度8-15位
* 4. 允许的格式:
* - +8613812345678
* - +44 20 7123 4567
* - +1 (800) 555-1234
*/
$pattern = '/^\+\d{1,4}[\d\s\(\)-]{8,}$/';
// 移除所有非必要字符后验证基础长度
$cleanNumber = preg_replace('/[^\d+]/', '', $value);
return preg_match($pattern, $value) &&
strlen($cleanNumber) >= 9 && // +号 + 至少8位数字
strlen($cleanNumber) <= 16; // E.164 最大长度15位 + +号
}
// 自定义验证规则 - 检查真实有效姓名
protected function checkEmptyString($value)
{
return trim($value) !== '';
}
}