PortalCategoryValidate.php
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\validate;
use app\admin\model\RouteModel;
use think\Validate;
class PortalCategoryValidate extends Validate
{
protected $rule = [
'name' => 'require',
'alias' => 'checkAlias',
];
protected $message = [
'name.require' => '分类名称不能为空',
];
protected $scene = [
// 'add' => ['user_login,user_pass,user_email'],
// 'edit' => ['user_login,user_email'],
];
// 自定义验证规则
protected function checkAlias($value, $rule, $data)
{
if (empty($value)) {
return true;
}
if (preg_match("/^\d+$/", $value)) {
return "别名不能为纯数字!";
}
$routeModel = new RouteModel();
if (isset($data['id']) && $data['id'] > 0) {
$fullUrl = $routeModel->buildFullUrl('portal/List/index', ['id' => $data['id']]);
} else {
$fullUrl = $routeModel->getFullUrlByUrl($data['alias']);
}
if (!$routeModel->exists($value, $fullUrl)) {
return true;
} else {
return "别名已经存在!";
}
}
}