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

65 lines
2.2 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
{
// 验证规则
protected $rule = [
'name' => 'require|max:255|checkEmptyString',
'phone' => 'require|checkInternationalPhone',
'content' => 'require|min:10|max:65535',
'vehicle_id' => 'integer',
'email' => 'email'
];
// 错误提示信息
protected $message = [
'name.require' => '姓名不能为空',
'name.max' => '姓名长度不能超过255个字符',
'name.checkEmptyString' => '姓名不能只包含空格',
'phone.require' => '联系电话不能为空',
'phone.checkInternationalPhone' => '请使用国际格式',
'content.require' => '内容不能为空',
'content.min' => '内容至少需要10个字符',
'content.max' => '内容长度超过最大限制',
'vehicle_id.integer' => '车辆ID必须为数字',
'email.email' => '邮箱格式不正确'
];
// 验证场景
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) !== '';
}
}