实例
<?php namespace app\index\controller; use app\index\model\Staff as StaffModel; use think\Controller; use think\db\Query; use app\index\validate\Staff as StaffValidate; use think\facade\Request; use think\Validate; /** * Created by PhpStorm. * User: lenovo * Date: 2019/3/23 * Time: 10:34 */ class Staff extends Controller { //验证器:类,使用户自定义的验证类 public function demo5(){ //用数组模拟表单提交过来的数据 $data = [ 'name'=> 'guoer', 'sex' => '0', 'age'=> '25', 'email'=>'sajkd@163.com' ]; $validate = new StaffValidate(); $result = $validate->check($data); if(false === $result){ halt($validate->getError()); } return '验证通过'; } //独立验证 public function demo6() { //think\validate //1、创建验证规则 $rule = [ 'name' => 'require|chsAlpha|length:2,20', 'sex' => 'require|integer|in:0,1', 'age' => 'require|integer|between:18,59', 'email' => 'require|email' ]; //验证提示 $message = [ 'name.require' => '姓名不能为空', 'name.chsAlpha' => '姓名必须为字母或汉字', 'name.length' => '姓名长度必须在3到20个字符之间', 'sex.require' => '性别不能为空', 'sex.integer' => '性别必须是整数', 'sex.in' => '性别必须是0或1', 'age.require' => '年龄不能为空', 'age.integer' => '年龄必须是整数', 'age.between' => '年龄必须在18到59之间', 'email.require' => '邮箱不能为空', 'email.email' => '邮箱格式不对', ]; //用数组模拟表单提交过来的数据 $validate = Validate::make($rule, $message); $data = [ 'name' => 'guoer', 'sex' => '3', 'age' => '25', 'email' => 'sajkd@163.com' ]; $result = $validate->check($data); if(false === $result){ halt($validate->getError()); } return '验证通过'; } //独立验证:使用验证对象操作 public function demo7(){ $validate = new Validate(); $data = [ 'name' => 'guoer', 'sex' => '1', 'age' => '125', 'email' => 'sajkd@163.com' ]; $result = $validate->rule( [ 'name' => 'require|chsAlpha|length:2,20', 'sex' => 'require|integer|in:0,1', 'age' => 'require|integer|between:18,59', 'email' => 'require|email' ] ) ->message([ 'name.require' => '姓名不能为空', 'name.chsAlpha' => '姓名必须为字母或汉字', 'name.length' => '姓名长度必须在3到20个字符之间', 'sex.require' => '性别不能为空', 'sex.integer' => '性别必须是整数', 'sex.in' => '性别必须是0或1', 'age.require' => '年龄不能为空', 'age.integer' => '年龄必须是整数', 'age.between' => '年龄必须在18到59之间', 'email.require' => '邮箱不能为空', 'email.email' => '邮箱格式不对', ]) ->check($data); if(false === $result){ halt($validate->getError()); } return '验证通过'; } //表单令牌验证 //准备一张表单 public function demo8(){ return $this->fetch(); } //验证通过表单令牌提交的表单 public function demo9(){ $data = Request::param(); //验证数据 $validate = Validate::make(); $result = $validate->rule(['email|邮箱'=>'require|email','password|密码'=>'require'])->check($data); if($result === false){ $this->error($validate->getError()); } $this->success('验证通过','admin'); } public function admin(){ return $this->display('欢迎进入网站管理后台'); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>用户登录</h3> <form action="{:url('demo9')}" method="post"> <p> <label for="email">邮箱</label> <input type="email" name="email" id="email"> </p> <p> <label for="password">密码</label> <input type="password" name="password" id="password"> </p> <!--//添加一个表单的隐藏域,将表单的令牌发送到服务器进行验证--> <input type="hidden" name="__token__" value="{$Request.token}"> <p> <button>登录</button> </p> </form> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例