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

97 lines
2.6 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 = [
2023-08-04 11:23:37 +08:00
'nickname' => 'require|min:3|max:32|checkName',
2023-06-19 14:32:30 +08:00
'pwd|密码' => 'require|min:6|max:64',
'email' => 'require',
'mobile' => 'require|mobile',
'captcha' => 'require',
2022-08-19 19:48:37 +08:00
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'nickname.require' => '用户名不能为空',
2023-08-04 11:23:37 +08:00
'nickname.min' => '用户名不能少于3个字符',
2023-06-19 14:32:30 +08:00
'nickname.max' => '用户名不能超过32个字符',
'nickname.checkName' => '用户名包含禁止注册字符',
'pwd.require' => '密码不能为空',
'pwd.min' => '密码不能少于6个字符',
'pwd.max' => '密码不能超过64个字符',
'email.require' => '邮箱不能为空',
'mobile.require' => '手机号不能为空',
'mobile.mobile' => '手机号格式不正确',
'captcha.require' => '验证码不能为空',
2022-08-19 19:48:37 +08:00
];
// 测试验证场景
protected $scene = [
2022-11-28 19:11:12 +08:00
'nickname' => ['nickname'],
2023-06-19 14:32:30 +08:00
'mobile' => ['mobile', 'captcha'],
'login' => ['nickname', 'pwd'],
2022-08-19 19:48:37 +08:00
];
/**
* 自定义验证规则
* @param $value
2023-07-03 10:08:34 +08:00
* @return bool
2022-11-28 19:11:12 +08:00
* @throws InvalidArgumentException
2022-08-19 19:48:37 +08:00
*/
2023-07-03 10:08:34 +08:00
protected function checkName($value): bool
2022-08-19 19:48:37 +08:00
{
$notAllow = saenv('user_reg_notallow');
$notAllow = explode(',', $notAllow);
2022-11-28 19:11:12 +08:00
if (in_array($value, $notAllow)) {
2023-07-03 10:08:34 +08:00
return false;
2022-08-19 19:48:37 +08:00
}
2023-06-19 14:32:30 +08:00
2022-08-19 19:48:37 +08:00
return true;
}
2023-06-19 14:32:30 +08:00
2023-07-03 10:08:34 +08:00
public function sceneAdd(): User
{
return $this->only(['nickname', 'pwd', 'email', 'mobile']);
}
public function sceneEdit(): User
{
return $this->only(['nickname', 'email']);
}
2023-06-19 14:32:30 +08:00
public function sceneRegister(): User
{
return $this->only(['nickname', 'pwd']);
}
public function scenePwd(): User
{
return $this->only(['pwd'])->append('pwd', 'confirm');
}
public function sceneMobile(): User
{
return $this->only(['mobile', 'captcha']);
}
2023-07-03 10:08:34 +08:00
}