如何高效运用ThinkPHP5.0验证类进行数据验证?
- 内容介绍
- 文章标签
- 相关推荐
本文共计457个文字,预计阅读时间需要2分钟。
通过一个实例,给家长们解释一下如何通过ThinkPHP5.0验证类的方法。
自定义验证类,需要继承Validate类。比如在home模块新建validate文件夹,再新建Test.php验证类,内容如下:
php
use think\validate\Validate;
class Test extends Validate{ protected $rule=[ 'name'=> 'require|max:25', 'age'=> 'number|between:1,120', ];
protected $message=[ 'name.require'=> '用户名不能为空', 'name.max'=> '用户名最多不能超过25个字符', 'age.number'=> '年龄必须是数字', 'age.between'=> '年龄必须在1到120之间', ];}
这样,我们就创建了一个名为Test的验证类,其中定义了两个规则:name和age。
本文共计457个文字,预计阅读时间需要2分钟。
通过一个实例,给家长们解释一下如何通过ThinkPHP5.0验证类的方法。
自定义验证类,需要继承Validate类。比如在home模块新建validate文件夹,再新建Test.php验证类,内容如下:
php
use think\validate\Validate;
class Test extends Validate{ protected $rule=[ 'name'=> 'require|max:25', 'age'=> 'number|between:1,120', ];
protected $message=[ 'name.require'=> '用户名不能为空', 'name.max'=> '用户名最多不能超过25个字符', 'age.number'=> '年龄必须是数字', 'age.between'=> '年龄必须在1到120之间', ];}
这样,我们就创建了一个名为Test的验证类,其中定义了两个规则:name和age。

