样式:
一、在控制器中开启验证器
protected $modelValidate = true; //开启validate,验证默认是fasle
protected $modelSceneeVaildate = true; //开启模型场景验证,默认是false(模型场景)
二、设置验证规则
位置:/application/admin/validate/pim/Test.php
<?php
namespace app\admin\validate\pim;
use think\Validate;
class Test extends Validate
{
/**
* 验证规则
*/
protected $rule = [
"depart_name" => "require",
"need_time" => "require",
"order_type" => "require"
];
/**
* 提示消息
*/
protected $message = [
"depart_name.require"=>"部门信息为必填项",
"need_time.require"=>"需求日期为必填项",
"order_type.require"=>"订单类型为必填项"
];
/**
* 验证场景
*/
protected $scene = [
'add' => ["depart_name","order_type"],
'edit' => ["need_time"],
];
}
内置规则可参见https://www.kancloud.cn/manual/thinkphp5/129356
注:
不需要使用任何内置规则在验证场景中写['']
为[]是默认全都有执行
/**
* 验证场景
*/
protected $scene = [
'add' => ["depart_name","order_type"],
'edit' => [''],
];
发表评论 取消回复