From 1ad3996786ff55cd7a81c185e4cbb2d35810170e Mon Sep 17 00:00:00 2001 From: "HDM58\\hdm58" Date: Thu, 11 Jan 2024 10:07:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B2=97=E4=BD=8D=E8=81=8C=E7=A7=B0=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BB=A5=E5=90=8E=EF=BC=8C=E5=86=8D=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=B7=B2=E7=BB=8F=E5=AD=98=E5=9C=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/user/validate/PositionCheck.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/app/user/validate/PositionCheck.php b/app/user/validate/PositionCheck.php index a535cb7..a1bd922 100644 --- a/app/user/validate/PositionCheck.php +++ b/app/user/validate/PositionCheck.php @@ -6,13 +6,28 @@ */ namespace app\user\validate; - +use think\facade\Db; use think\Validate; class PositionCheck extends Validate { + // 自定义验证规则 + protected function checkUnique($value, $rule, $data) + { + [$table, $field, $id] = explode(',', $rule); + $idField = $id ?: 'id'; + $idValue = $data[$idField] ?? null; + $map = [ + [$field, '=', $value], + ]; + if (!is_null($idValue)) { + $map[] = [$idField, '<>', $idValue]; + } + $map[] = ['status', '=', 1]; + return !Db::name($table)->where($map)->count(); + } protected $rule = [ - 'title' => 'require|unique:position', + 'title' => 'require|checkUnique:Position,title,id', 'work_price' => 'require|number', 'group_id' => 'require', 'id' => 'require' @@ -20,7 +35,7 @@ class PositionCheck extends Validate protected $message = [ 'title.require' => '岗位名称不能为空', - 'title.unique' => '同样的岗位名称已经存在', + 'title.checkUnique' => '同样的岗位名称已经存在', 'work_price.require' => '岗位工时单价不能为空', 'work_price.number' => '岗位工时单价只能是整数', 'group_id.require' => '至少要选择一个角色权限',