webman:用think-validate做验证器(v1.5.7)
一,官方文档地址:
https://www.workerman.net/doc/webman/components/validation.html
二,安装组件:
1,安装
liuhongdi@lhdpc:/data/webman/imageadmin$ composer require topthink/think-validate
2,安装后查看版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show topthink/think-validate
name : topthink/think-validate
descrip. : think validate
keywords :
versions : * v2.0.2
...
三,代码:
1,app/validate/ImageList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?php namespace app\validate; use think\Validate; class ImageList extends Validate { /** * 定义验证规则 * 格式:'字段名' => ['规则1','规则2'...] * * @var array */ protected $rule = [ 'categoryId' => 'require|number|gt:0' , 'channelId' => 'require|length:4|alphaNum' , ]; /** * 定义错误信息 * 格式:'字段名.规则名' => '错误信息' * * @var array */ protected $message = [ 'categoryId.require' => '分类id必须' , 'categoryId.number' => '分类id需要是整数' , 'categoryId.gt' => '分类id需大于0' , 'channelId.require' => '频道id必须' , 'channelId.length' => '频道id长度不为4' , 'channelId.alphaNum' => '频道id需由字母和数字构成' , ]; } |
2,调用
app/controller/ImageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php namespace app\controller; use support\Request; use app\result\Result; use support\Log; use think\facade\Db; use think\facade\Cache; use app\model\Comment as CommentModel; class ImageController { //图片的列表 public function list(Request $request ) { $validate = new \app\validate\ImageList; $res = $validate ->check( $request ->get()); //检查是否合法 if ( $res == false) { $error = $validate ->getError(); return Result::ErrorCode(422, $error ); } //获取数据 $path = '_image_imageborder' ; $comObj = new CommentModel(); $rows = $comObj ->list( $path ); return Result::Success( $rows ); } } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/08/23/webman-yong-thinkvalidate-zuo-yan-zheng-qi-v1-5-7/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,测试效果:
五,查看webman版本:
liuhongdi@lhdpc:/data/webman/imageadmin$ composer show workerman/webman-framework
name : workerman/webman-framework
descrip. : High performance HTTP Service Framework.
keywords : High Performance, http service
versions : * v1.5.7
...