验证器部分
实例
<?php namespace app\validate; use think\Validate; class User extends Validate{ // 验证规则 protected $rule = [ 'name' => 'require|min:5|max:15', 'email' => 'require|email|length:5,40', 'password' => 'require|length:6,50', ]; // 自定义错误信息 protected $message = [ 'name.require' => '姓名不能为空', 'name.min' => '姓名不能少于5字符', 'name.max' => '姓名不能大于15个字符', 'email.require' => 'Email不能为空', 'email.email' => 'Email格式不正确', 'email.length' => 'Email长度在5-40字符之间', 'password.require' => '密码不能为空', 'password.length' => '密码长度在20-50之间', ]; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
控制器部分
实例
<?php namespace app\index\controller; use think\Controller; use think\Request; use app\validate\User; class Verify extends Controller { /** * 显示资源列表 * * @return \think\Response */ public function index(User $user) { $data = ['name' => 'gakkispy', 'email' => 'mail@php.cn', 'password' => 'password']; $res= $user->check($data); return $res ? 'success' : $user->getError(); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例