- A+
项目安装就我做声明了,自己按照文档安装即可
官方案例:
可以直接在验证器类中使用message属性定义错误提示信息,例如:
namespace app\validate; use think\Validate; class User extends Validate { protected $rule = [ 'name' => 'require|max:25', 'age' => 'number|between:1,120', 'email' => 'email', ]; protected $message = [ 'name.require' => '名称必须', 'name.max' => '名称最多不能超过25个字符', 'age.number' => '年龄必须是数字', 'age.between' => '年龄只能在1-120之间', 'email' => '邮箱格式错误', ]; }
数据验证:
在需要进行User验证的控制器方法中,添加如下代码即可:
<?php namespace app\controller; use app\validate\User; use think\exception\ValidateException; class Index { public function index() { try { validate(User::class)->check([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com', ]); } catch (ValidateException $e) { // 验证失败 输出错误信息 dump($e->getError()); } } }
这中验证方式,需要在每个控制器中都要进行编写,感觉怪怪的,那么有没有一种简便的方法呢,请往下看
1、先在controller同级目录新建validate目录
2、在validate目录中新建AbstractValidate.php(名字可以随便取,看自己心情)
AbstractValidate.php
<?php namespace app\index\validate; use think\Validate; class AbstractValidate extends Validate { public function getErrors($data) { $this->check($data); return $this->getError(); } public function __set($name, $value) { // TODO: Implement __set() method. $this->rule[$name] = $value; } }
3、然后在同级目录新建验证规则,例:UserValidate.php(名字可以随便取,看自己心情),验证规则可参考官方手册
UserValidate.php
<?php namespace app\index\validate; class UserValidate extends AbstractValidate { protected $rule = [ 'username' => 'require|length:4,25', 'age' => 'require|integer', 'addr' => 'require' ]; protected $message = [ 'username.require' => '用户名必填', 'username.length' => '用户名介于4~20个字符', 'age.require' => '年龄必填', 'age.integer' => '年龄必须是整数', 'addr.require' => '地址必填' ]; }
4、验证规则建好之后,那开始创建验证吧,在同级目录新建request目录(名字也可随便取,看心情),并在该目录新增FormRequest.php(随便啦)。
FormRequest.php
<?php namespace app\index\request; use app\Request; use think\exception\HttpResponseException; abstract class FormRequest extends Request { public function __construct(array $option = []) { parent::__construct(); if ($this->withServer($_SERVER)->isAjax(true) && $err = $this->validate()){ throw new HttpResponseException( json([ "code" => 10001, "message" => $err, "info" => "" ]) ); } } }
5、然后在该目录下新增User的request文件,进行UserValitdate中验证规则的验证,并返回错误信息,例:
UserRequest.php
<?php namespace app\index\request; use app\index\validate\UserValidate; use think\facade\Request; class UserRequest extends FormRequest { public function validate() { return (new UserValidate())->getErrors(Request::post()); } }
到现在为止,验证的信息都已经准备就绪了,那么我们结合例子来看看效果吧
Index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/jquery.form/3.09/jquery.form.js"></script> </head> <body> <form action="{:url('index/Index/add')}" method="put" id="form"> <input type="text" name="username"> <input type="text" name="age"> <input type="text" name="addr"> <input type="submit" value="提交" /> </form> <script> var options={ beforeSubmit:function(){ //提交前的验证 }, success:function(data){ console.log(data); //成功之后的操作 } }; $("#form").ajaxForm(options); </script> </body> </html>
控制器:
<?php namespace app\index\controller; use app\index\request\UserRequest; use think\response\Json; class Index { public function index() { return view(); } /** * add * @param UserRequest $userRequest * @return Json */ public function add(UserRequest $userRequest){ //Do Something here } }
测试结果:
//对应username.length
//对应age.intege
//对应addr.require
当然了。方法归方法,具体的逻辑与应用场景视情况而定,自行修改。