75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
||
// +------------------------------------------------+
|
||
// |http://www.cjango.com |
|
||
// +------------------------------------------------+
|
||
// | 修复BUG不是一朝一夕的事情,等我喝醉了再说吧! |
|
||
// +------------------------------------------------+
|
||
// | Author: 小陈叔叔 <Jason.Chen> |
|
||
// +------------------------------------------------+
|
||
namespace app\common\validate;
|
||
|
||
use think\Config;
|
||
use think\Db;
|
||
use think\Validate;
|
||
|
||
class Category extends Validate
|
||
{
|
||
/**
|
||
* 验证规则
|
||
* @var array
|
||
*/
|
||
protected $rule = [
|
||
'title' => 'require|length:1,100',
|
||
'model' => 'require|checkModel:lvyang|checkExtends:lvyang',
|
||
'pid' => 'require|integer',
|
||
'sort' => 'require|integer',
|
||
'description' => 'length:0,255',
|
||
];
|
||
|
||
/**
|
||
* 错误提示消息
|
||
* @var array
|
||
*/
|
||
protected $message = [
|
||
'title.require' => '分类名称必须填写',
|
||
'title.length' => '分类名称请保持在:1-:2字符之间',
|
||
'model.require' => '分类模型必须选择',
|
||
'model.checkModel' => '分类选择有误',
|
||
'model.checkExtends' => '分类模型必须与上级模型一致',
|
||
'pid.require' => '上级分类必须选择',
|
||
'pid.integer' => '上级分类选择错误',
|
||
'sort.require' => '排序必须填写',
|
||
'sort.integer' => '排序必须是整数',
|
||
'description.length' => '分类备注请保持在:rule字符以内',
|
||
];
|
||
|
||
/**
|
||
* 检测模型是否合法
|
||
* @param [type] $value 检查的值
|
||
* @param [type] $rule 规则
|
||
* @param [type] $data 数据
|
||
* @return [type] 返回结果
|
||
*/
|
||
protected function checkModel($value, $rule, $data)
|
||
{
|
||
$keys = array_keys(Config::get('category_model'));
|
||
return in_array($value, $keys);
|
||
}
|
||
|
||
/**
|
||
* 检测继承分类是否是相同的模型
|
||
* @param [type] $value 检查的值
|
||
* @param [type] $rule 规则
|
||
* @param [type] $data 数据
|
||
* @return [type] 返回结果
|
||
*/
|
||
protected function checkExtends($value, $rule, $data)
|
||
{
|
||
if (0 != $data['pid']) {
|
||
return $value == Db::name('Category')->where('id', $data['pid'])->value('model');
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
}
|