验证器
<?php namespace app\index\validate; use think\Validate; class User extends Validate { /** * 定义验证规则 * 格式:'字段名' => ['规则1','规则2'...] * * @var array */ protected $rule = [ 'user_name' => 'unique:user', ]; /** * 定义错误信息 * 格式:'字段名.规则名' => '错误信息' * * @var array */ protected $message = [ 'user_name.unique' => '用户名必须唯一', ]; }
控制器调用事例:
public function insert() { $data = ['user_name'=>'bootstrap','sex'=>1,'age'=>52,'sallary'=>4300,'insert_time'=>'2019-3-16']; $validate = new UserValidate(); if(!$validate->check($data)){ dump($validate->getError());die; } }