88 lines
3.1 KiB
PHP
88 lines
3.1 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\admin\validate\v1;
|
|
|
|
use app\admin\model\v1\ArticleModel;
|
|
use think\facade\Db;
|
|
use think\Validate;
|
|
|
|
class ArticleCategoryValidate extends Validate
|
|
{
|
|
/**
|
|
* 定义验证规则
|
|
* 格式:'字段名' => ['规则1','规则2'...]
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $rule = [
|
|
'id' => 'require|integer',
|
|
'language_id' => 'require|integer',
|
|
'name' => 'require|unique:article_category,name^language_id|max:64',
|
|
'pid' => 'integer"different:id|checkPidNotBeChildren', // 验证pid
|
|
'sort' => 'require|integer',
|
|
'is_show' => 'require|in:0,1',
|
|
'seo_title' => 'max:255',
|
|
'seo_keywords' => 'max:255',
|
|
'seo_desc' => 'max:255',
|
|
];
|
|
|
|
/**
|
|
* 定义错误信息
|
|
* 格式:'字段名.规则名' => '错误信息'
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $message = [
|
|
'id.require' => '分类ID不能为空',
|
|
'id.integer' => '分类ID必须为整数',
|
|
'language_id.require' => '语言ID不能为空',
|
|
'language_id.integer' => '语言ID必须为整数',
|
|
'name.require' => '分类名称不能为空',
|
|
'name.unique' => '分类名称已存在',
|
|
'name.max' => '分类名称最多64个字符',
|
|
'pid.integer' => '父级分类ID必须为整数',
|
|
'pid.different' => '父级分类ID不能为自身',
|
|
'pid.checkPidNotBeChildren' => '父级分类不能为子分类',
|
|
'sort.require' => '排序不能为空',
|
|
'sort.integer' => '排序必须为整数',
|
|
'is_show.require' => '是否显示不能为空',
|
|
'is_show.in' => '是否显示值必须为0或1',
|
|
'seo_title.max' => 'SEO标题最多255个字符',
|
|
'seo_keywords.max' => 'SEO关键词最多255个字符',
|
|
'seo_desc.max' => 'SEO描述最多255个字符',
|
|
];
|
|
|
|
// 验证pid
|
|
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
|
{
|
|
if ($value == 0) {
|
|
return true;
|
|
}
|
|
$table_name = (new ArticleModel)->getTable();
|
|
$children = Db::query(
|
|
preg_replace(
|
|
'/\s+/u',
|
|
' ',
|
|
"WITH RECURSIVE article_tree_by AS (
|
|
SELECT a.id, a.pid FROM $table_name a WHERE a.id = {$data['id']}
|
|
UNION ALL
|
|
SELECT k.id, k.pid FROM $table_name k INNER JOIN article_tree_by t ON t.id = k.pid
|
|
)
|
|
SELECT id FROM article_tree_by WHERE id <> {$data['id']};"
|
|
)
|
|
);
|
|
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// 新增分类验证场景
|
|
protected function sceneAdd()
|
|
{
|
|
$this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
|
}
|
|
}
|