refactor: 修改产品分类中tco分类绑定验证
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 4s
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 4s
This commit is contained in:
@@ -23,7 +23,7 @@ class ProductCategoryValidate extends Validate
|
||||
'name' => 'require|max:64',
|
||||
'icon' => 'max:125',
|
||||
'desc' => 'max:255',
|
||||
'related_tco_category' => 'string',
|
||||
'related_tco_category' => 'string|checkRelatedTcoCategory',
|
||||
'sort' => 'integer',
|
||||
'level' => 'integer',
|
||||
'is_show' => 'in:0,1',
|
||||
@@ -39,25 +39,26 @@ 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个字符',
|
||||
'desc.max' => '描述最多不能超过255个字符',
|
||||
'related_tco_category.string' => '关联TCO分类格式错误',
|
||||
'sort.integer' => '排序格式错误',
|
||||
'level.integer' => '级别格式错误',
|
||||
'is_show.in' => '是否显示格式错误',
|
||||
'seo_title.max' => 'SEO标题最多不能超过255个字符',
|
||||
'seo_keywords.max' => 'SEO关键字最多不能超过255个字符',
|
||||
'seo_desc.max' => 'SEO描述最多不能超过255个字符',
|
||||
'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个字符',
|
||||
'desc.max' => '描述最多不能超过255个字符',
|
||||
'related_tco_category.string' => '关联TCO分类格式错误',
|
||||
'related_tco_category.checkRelatedTcoCategory' => '该TCO分类已绑定',
|
||||
'sort.integer' => '排序格式错误',
|
||||
'level.integer' => '级别格式错误',
|
||||
'is_show.in' => '是否显示格式错误',
|
||||
'seo_title.max' => 'SEO标题最多不能超过255个字符',
|
||||
'seo_keywords.max' => 'SEO关键字最多不能超过255个字符',
|
||||
'seo_desc.max' => 'SEO描述最多不能超过255个字符',
|
||||
];
|
||||
|
||||
// 验证pid
|
||||
@@ -84,4 +85,32 @@ class ProductCategoryValidate extends Validate
|
||||
{
|
||||
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
||||
}
|
||||
|
||||
// 验证related_tco_category
|
||||
protected function checkRelatedTcoCategory($value, $rule, $data = [])
|
||||
{
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$val = ProductCategoryModel::where(function($query) use($value) {
|
||||
$arr = explode(",", $value);
|
||||
foreach ($arr as $v) {
|
||||
$query->whereFindInSet('related_tco_category', $v, 'OR');
|
||||
}
|
||||
})->column('id');
|
||||
if (!empty($val)) {
|
||||
$size = count($val);
|
||||
// 如果存在超过一个,直接验证失败
|
||||
if ($size > 1) {
|
||||
return false;
|
||||
}
|
||||
// 如果存在一个,并存在的id不为自身,验证失败(考虑更新场景查到自身情况)
|
||||
if ($size == 1 && $val[0] != $data['id']) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user