fastadmin验证唯一性 排除自身

这是在edit方法里重写

 

staff是表名,username是字段名,$ids是当前数据的id

        try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)
                            ->validate(
                                [
                                    'username|用户名' => [
                                        'require',
                                        'alphaNum',
                                        'length:6,25',
                                        'unique' => 'staff,username,' . $ids . ''
                                    ],
                                    'password|密码' => ['require', 'alphaNum', 'length:6,25']
                                ]
                            );
                        // $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {

 

posted @ 2020-07-01 20:00  一颗糊涂淡  阅读(1976)  评论(0编辑  收藏  举报