<?php
//验证规则
namespace app\index\validate;
use think\Validate;
class Guize extends Validate
{
protected $rule = [
'name' => 'require|length:4,15',
'age' => 'require|between:18,45',
];
protected $message = [
'name.require' =>'姓名不可以为空',
'name.length'=> '姓名长度必须在4到15个字符之间',
'age.require' => '年龄不能为空',
'age.between' => '年龄必须在18到45岁之间'
];
}?>
<?php
//简单的不能再简单了的数据验证类
namespace app\index\controller;
use think\Controller;
class User extends Controller
{
public function test(){
$data = ['name' =>'12341',
'age' => 19
];
$vali = new \app\index\validate\Guize;
if(!$vali -> check($data)){
dump($vali->getError());
}else{
return '验证通过';
}
}
}?>