更新
This commit is contained in:
@@ -31,7 +31,7 @@ class DeptValidate extends BaseValidate
|
||||
protected $rule = [
|
||||
'id' => 'require|checkDept',
|
||||
'pid' => 'require|integer',
|
||||
'name' => 'require|unique:'.Dept::class.'|length:1,30',
|
||||
'name' => 'require|length:1,30|checkDeptNameUnderParent',
|
||||
'status' => 'require|in:0,1',
|
||||
'sort' => 'egt:0',
|
||||
];
|
||||
@@ -41,7 +41,7 @@ class DeptValidate extends BaseValidate
|
||||
'id.require' => '参数缺失',
|
||||
'name.require' => '请填写部门名称',
|
||||
'name.length' => '部门名称长度须在1-30位字符',
|
||||
'name.unique' => '部门名称已存在',
|
||||
'name.checkDeptNameUnderParent' => '同级部门下名称已存在',
|
||||
'sort.egt' => '排序值不正确',
|
||||
'pid.require' => '请选择上级部门',
|
||||
'pid.integer' => '上级部门参数错误',
|
||||
@@ -113,6 +113,28 @@ class DeptValidate extends BaseValidate
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同级(同一 pid)下部门名称不可重复;不同上级允许同名
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $rule
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function checkDeptNameUnderParent($value, $rule, array $data = [])
|
||||
{
|
||||
$name = trim((string) $value);
|
||||
if ($name === '') {
|
||||
return true;
|
||||
}
|
||||
$pid = (int) ($data['pid'] ?? 0);
|
||||
$query = Dept::where(['name' => $name, 'pid' => $pid]);
|
||||
if (!empty($data['id'])) {
|
||||
$query->where('id', '<>', (int) $data['id']);
|
||||
}
|
||||
|
||||
return $query->count() === 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 校验能否删除
|
||||
|
||||
Reference in New Issue
Block a user