48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* @copyright Copyright (c) 2021 勾股工作室
|
|
* @license https://opensource.org/licenses/GPL-2.0
|
|
* @link https://www.gougucms.com
|
|
*/
|
|
|
|
namespace app\admin\validate;
|
|
|
|
use think\facade\Db;
|
|
use think\Validate;
|
|
|
|
class KeywordsCheck extends Validate
|
|
{
|
|
protected $rule = [
|
|
'title' => 'require|checkUnique',
|
|
'id' => 'require',
|
|
];
|
|
|
|
protected $message = [
|
|
'title.require' => '关键字名称不能为空',
|
|
'title.checkUnique' => '同样的关键字名称已经存在',
|
|
'id.require' => '缺少更新条件',
|
|
];
|
|
|
|
protected $scene = [
|
|
'add' => ['title'],
|
|
'edit' => ['id', 'title'],
|
|
];
|
|
|
|
//自定义验证规则
|
|
protected function checkUnique($value, $rule, $data)
|
|
{
|
|
if (isset($data['id'])) {
|
|
$unique = Db::name('keywords')->where([['id', '<>', $data['id']], ['title', '=', $value], ['status', '>=', 0]])->value('id');
|
|
} else {
|
|
$unique = Db::name('keywords')->where([['title', '=', $value], ['status', '>=', 0]])->value('id');
|
|
}
|
|
|
|
if ($unique) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|