岗位职称删除以后,再添加提示已经存在的问题修复

This commit is contained in:
HDM58\hdm58 2024-01-11 10:07:51 +08:00
parent 2c2a03608d
commit 1ad3996786

View File

@ -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' => '至少要选择一个角色权限',