zzwy2/app/validate/Article.php

74 lines
1.7 KiB
PHP
Executable File

<?php
namespace app\validate;
use think\Validate;
class Article extends Validate
{
protected $rule = [
'category_id|栏目' => 'require|>:0',
'title|标题' => 'require|max:255',
// 'summary|摘要' => '',
'route|路由' => "routeValidate"
];
protected $message = [
'model_id' => '所属模型必需选择',
'route.routeValidate' => '路由不符合指定规范',
'route.unique' => '路由已存在',
];
protected function routeValidate($value)
{
$chars =preg_replace('/\s+/','',
"/^(
[\/]{1}
(
(
([A-Za-z0-9_-]*[A-Za-z_-]+)
|
([A-Za-z_-]+[A-Za-z0-9_-]*)
)
|
(
([A-Za-z0-9_-]+[A-Za-z_-]+[A-Za-z0-9_-]+)
|
([A-Za-z_-]+[A-Za-z0-9_-]+[A-Za-z_-]+)
)
)
)+
$/");
if ( preg_match($chars, $value)){
return true;
}else{
return "路由不符合指定规范";
}
}
// edit 验证场景定义
public function sceneEdit()
{
$id = input('id/d', 0);
return $this->append('route', 'unique:article,route,' .$id . '|unique:category,route' . '|unique:special_route,route' );
}
public function sceneAdd()
{
return $this->append('route', 'unique:category,route|unique:article,route|unique:special_route,route');
}
}