Files
swiftadmin/app/common/validate/system/User.php

67 lines
1.8 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
declare(strict_types=1);
namespace app\common\validate\system;
2022-11-28 19:11:12 +08:00
use Psr\SimpleCache\InvalidArgumentException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
2022-08-19 19:48:37 +08:00
use think\Validate;
class User extends Validate
{
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'test_filed' => 'max:255',
'nickname' => 'require|min:2|max:12|filters|chsAlphaNum',
'pwd|密码' => 'require|min:6|max:64',
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'nickname.require' => '用户名不能为空',
'nickname.min' => '用户名不能少于2个字符',
'nickname.max' => '用户名不能超过12个字符',
'nickname.filters' => '用户名包含禁止注册字符',
'nickname.chsAlphaNum' => '用户名只能是汉字、字母和数字',
'test_filed.max' => '测试场景用',
];
// 测试验证场景
protected $scene = [
2022-11-28 19:11:12 +08:00
'test' => ['test_filed'],
'nickname' => ['nickname'],
2022-08-19 19:48:37 +08:00
];
/**
* 自定义验证规则
* @param $value
* @return bool
2022-11-28 19:11:12 +08:00
* @throws InvalidArgumentException
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-08-19 19:48:37 +08:00
*/
protected function filters($value): bool
{
$notAllow = saenv('user_reg_notallow');
$notAllow = explode(',', $notAllow);
2022-11-28 19:11:12 +08:00
if (in_array($value, $notAllow)) {
return false;
2022-08-19 19:48:37 +08:00
}
return true;
}
}