Compare commits
3 Commits
cd98608188
...
7de54e5550
| Author | SHA1 | Date | |
|---|---|---|---|
| 7de54e5550 | |||
| e7ea53c78e | |||
| 1ad2f9b016 |
@@ -82,7 +82,7 @@ class ProductCategory
|
||||
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
||||
|
||||
$validate = new ProductCategoryValidate;
|
||||
if (!$validate->check($data)) {
|
||||
if (!$validate->scene('add')->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use app\admin\model\v1\SysNavigationItemModel;
|
||||
use think\facade\Db;
|
||||
use think\Validate;
|
||||
|
||||
class NavigationItemValidate extends Validate
|
||||
@@ -16,7 +18,7 @@ class NavigationItemValidate extends Validate
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'nav_id' => 'require|integer',
|
||||
'pid' => 'integer',
|
||||
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||
'name' => 'require|max:64',
|
||||
'icon' => 'max:64',
|
||||
'link_type' => 'require|integer',
|
||||
@@ -33,23 +35,51 @@ class NavigationItemValidate extends Validate
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须为整数',
|
||||
'nav_id.require' => '导航ID不能为空',
|
||||
'nav_id.integer' => '导航ID必须为整数',
|
||||
'pid.integer' => '父级ID必须为整数',
|
||||
'name.require' => '导航名称不能为空',
|
||||
'name.max' => '导航名称最多不能超过64个字符',
|
||||
'icon.max' => '图标最多不能超过64个字符',
|
||||
'link_type.require' => '链接类型不能为空',
|
||||
'link_type.integer' => '链接类型必须为整数',
|
||||
'link.max' => '链接最多不能超过255个字符',
|
||||
'sort.integer' => '排序必须为整数',
|
||||
'blank.in' => '是否新窗口打开只能是0或1',
|
||||
'status.require' => '状态不能为空',
|
||||
'status.in' => '状态只能是1或-1',
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须为整数',
|
||||
'nav_id.require' => '导航ID不能为空',
|
||||
'nav_id.integer' => '导航ID必须为整数',
|
||||
'pid.integer' => '父级ID必须为整数',
|
||||
'pid.different' => '父级ID不能为自身',
|
||||
'pid.checkPidNotBeChildren' => '父级ID不能为自身的子导航',
|
||||
'name.require' => '导航名称不能为空',
|
||||
'name.max' => '导航名称最多不能超过64个字符',
|
||||
'icon.max' => '图标最多不能超过64个字符',
|
||||
'link_type.require' => '链接类型不能为空',
|
||||
'link_type.integer' => '链接类型必须为整数',
|
||||
'link.max' => '链接最多不能超过255个字符',
|
||||
'sort.integer' => '排序必须为整数',
|
||||
'blank.in' => '是否新窗口打开只能是0或1',
|
||||
'status.require' => '状态不能为空',
|
||||
'status.in' => '状态只能是1或-1',
|
||||
];
|
||||
|
||||
// 验证pid
|
||||
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||
{
|
||||
if ($value == 0) {
|
||||
return true;
|
||||
}
|
||||
$table_name = (new SysNavigationItemModel)->getTable();
|
||||
$children = Db::query(
|
||||
preg_replace(
|
||||
'/\s+/u',
|
||||
' ',
|
||||
"WITH RECURSIVE 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 tree_by t ON t.id = k.pid
|
||||
)
|
||||
SELECT id FROM tree_by WHERE id <> {$data['id']};"
|
||||
)
|
||||
);
|
||||
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 定义场景
|
||||
protected $scene = [
|
||||
'add' => ['nav_id', 'pid', 'name', 'icon', 'link_type', 'link', 'sort', 'blank', 'status'],
|
||||
|
||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use app\admin\model\v1\ProductCategoryModel;
|
||||
use think\facade\Db;
|
||||
use think\Validate;
|
||||
|
||||
class ProductCategoryValidate extends Validate
|
||||
@@ -14,8 +16,10 @@ class ProductCategoryValidate extends Validate
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'language_id' => 'require|integer',
|
||||
'unique_id' => 'require',
|
||||
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||
'name' => 'require|max:64',
|
||||
'icon' => 'max:125',
|
||||
'desc' => 'max:255',
|
||||
@@ -35,9 +39,14 @@ class ProductCategoryValidate extends Validate
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须为整数',
|
||||
'language_id.require' => '语言ID不能为空',
|
||||
'language_id.integer' => '语言ID必须为整数',
|
||||
'unique_id.require' => '唯一标识不能为空',
|
||||
'pid.different' => '父级ID不能为自身',
|
||||
'pid.checkPidNotBeChildren' => '父级ID不能为自身的子分类',
|
||||
'pid.integer' => '父级ID必须为整数',
|
||||
'name.require' => '名称不能为空',
|
||||
'name.max' => '名称最多不能超过64个字符',
|
||||
'icon.max' => '图标最多不能超过125个字符',
|
||||
@@ -50,4 +59,36 @@ class ProductCategoryValidate extends Validate
|
||||
'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 ProductCategoryModel)->getTable();
|
||||
$children = Db::query(
|
||||
preg_replace(
|
||||
'/\s+/u',
|
||||
' ',
|
||||
"WITH RECURSIVE 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 tree_by t ON t.id = k.pid
|
||||
)
|
||||
SELECT id FROM tree_by WHERE id <> {$data['id']};"
|
||||
)
|
||||
);
|
||||
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 新增场景
|
||||
public function sceneAdd()
|
||||
{
|
||||
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use app\admin\model\v1\SysMenuModel;
|
||||
use think\facade\Db;
|
||||
use think\Validate;
|
||||
|
||||
class SysMenuValidate extends Validate
|
||||
@@ -15,7 +17,7 @@ class SysMenuValidate extends Validate
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'pid' => 'integer',
|
||||
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||
'title' => 'require|max:64',
|
||||
'name' => 'require|unique:sys_menu|max:64',
|
||||
'path' => 'require|max:128',
|
||||
@@ -43,6 +45,8 @@ class SysMenuValidate extends Validate
|
||||
'id.require' => '菜单ID不能为空',
|
||||
'id.integer' => '菜单ID必须为整数',
|
||||
'pid.integer' => '父级菜单ID必须为整数',
|
||||
'pid.different' => '父级菜单ID不能为自身',
|
||||
'pid.checkPidNotBeChildren' => '父级菜单不能为自身的子菜单',
|
||||
'title.require' => '菜单名称不能为空',
|
||||
'title.max' => '菜单名称最多64个字符',
|
||||
'name.require' => '菜单Name不能为空',
|
||||
@@ -64,9 +68,35 @@ class SysMenuValidate extends Validate
|
||||
'menu_ability_permission.*.sort.integer' => '菜单能力权限排序必须为整数',
|
||||
];
|
||||
|
||||
// 验证pid
|
||||
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||
{
|
||||
if ($value == 0) {
|
||||
return true;
|
||||
}
|
||||
$table_name = (new SysMenuModel)->getTable();
|
||||
$children = Db::query(
|
||||
preg_replace(
|
||||
'/\s+/u',
|
||||
' ',
|
||||
"WITH RECURSIVE menu_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 menu_tree_by t ON t.id = k.pid
|
||||
)
|
||||
SELECT id FROM menu_tree_by WHERE id <> {$data['id']};"
|
||||
)
|
||||
);
|
||||
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// 新增验证场景
|
||||
public function sceneCreate()
|
||||
{
|
||||
return $this->remove('id', 'require|integer');
|
||||
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user