首页 > 编程知识 正文

自喷压力容器定义,验证分离定律自交法

时间:2023-05-03 20:05:39 阅读:249025 作者:3599

tp5.1 tp6自定义unique验证

###新增一个继承与Think/Validate 的方法:

// A code block<?phpdeclare (strict_types=1);namespace validate;use thinkfacadeDb;use thinkhelperStr;use thinkValidate;class baseValidate extends Validate{ /** * @var string 关联数据表名称 */ protected $name; /** * 数据表主键 复合主键使用数组定义 * @var string|array */ protected $pk = 'id'; /** * @var array */ protected $uniqueWhere = []; /** * @return string 获取当前验证器对应的数据库名称 */ public function getName() { if (empty($this->name)) { $name = str_replace('\', '/', static::class); $this->name = Str::snake(basename($name)); if (strpos($this->name, 'validate')) { [$name, $validate] = explode('validate', $this->name); $this->name = Str::snake($name); } } return $this->name; } /** * 添加场景 * @return baseValidate */ public function sceneAdd() { return $this->only(array_diff($this->getSchemas(), [$this->pk])); } /** * 编辑场景 * @return baseValidate */ public function sceneEdit() { return $this->only($this->getSchemas()); } /** * 设置自定义的unique方法的其他条件 * @example ['state' => 1] * @param array $param */ public function setUniqueWhere($param = []): void { $this->uniqueWhere = $param; } /** * 获取自定义的unique方法的其他条件 * @example ['state' => 1] * @return array */ public function getUniqueWhere(): array { return $this->uniqueWhere; } /** * 验证数据是否已经存在数据库中 * 区别新增和编辑操作,如果是新增判断时候已经存在数据 * 编辑时验证是否改动当前数据,如果数据改动才验证数据是否已经存在 * @param mixed $value 当前验证字段值 * @param mixed $rule 当前验证字段规则 * @param array $data 当前验证器的数据 * @param mixed $field 当前验证的字段 * @return bool|string * @throws thinkdbexceptionDataNotFoundException * @throws thinkdbexceptionDbException * @throws thinkdbexceptionModelNotFoundException */ public function checkUnique($value, $rule, $data, $field) { $uniqueRule = $this->getName(); if (!empty($data[$this->pk])) { $info = $this->table->failException()->find($this->pk); if ($info[$field] !== $value) { return true; } } if (!empty($this->uniqueWhere)) { $uniqueRule = ',' . build_query($this->uniqueWhere); } if (!$this->unique($value, $uniqueRule, $data)) { return '信息已经存在,请确认'; } return true; }} 下面我们有个客户的验证器 // 客户验证器<?phpdeclare (strict_types=1);namespace validatecustom;use validatebaseValidate;class customValidate extends baseValidate{ protected $name = 'custom'; /** * 额外的unique验证参数 * @var int[] */ protected $uniqueWhere = [ 'state' => 1, ]; /** * @var array */ protected $rule = [ 'id|客户信息' => 'require|number|max:20', 'mobile|手机号码' => 'require|mobile|checkUnique', 'email|邮箱地址' => 'require|email|max:150|checkUnique', 'sex|性别' => 'require|number', 'birthday|生日' => 'date', 'login|登录账户' => 'require|max:32|checkUnique', 'true_name|真实姓名' => 'require|max:24', 'nickname|昵称' => 'max:24', 'url|个人网址' => 'max:100', 'avatar|头像地址' => 'max:255', 'signature|个性签名' => 'max:255', 'qq|QQ' => 'number|max:12', 'wx|微信号' => 'max:32', 'remark|备注信息' => 'max:255', ];}

上面的customValidate继承自baseValidate

在customValidate类中 的mobile使用了自定义的checkUnique验证方法,在未输入主键信息相关的数据的时候(相当于新增数据),只会去验证当前的方法是否已经存在相同的记录:类似于:Db::name($this->getName)->where('mobile', $value)->where('state',1)->find();我们看到实际上查询条件多了一个state=>1的条件,是因为我们定义了$uniqueWhere的值,他会自动的添加查询的条件当我们有主键对应的值输入的时候,首先去查看主键信息相关参数是否存在,如果主键相关信息存在,并且当前的数据字段值等于查询出的信息字段值的时候,则直接返回为true。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。