Home >Backend Development >PHP Tutorial >How to use thinkphp5.0 verification class
This article mainly introduces how to use the thinkphp5.0 verification class. It has a certain reference value. Now I share it with you. Friends in need can refer to it.
I will explain it to you through an example. If you pass the thinkphp5.0 verification class method.
Customize the validation class and need to inherit the Validate class
For example, create a new validate folder in the home module, and then create a new Test.php validation class with the following content:
<?php namespace app\home\validate; use think\Validate; class Test extends Validate { protected $rule = [ 'name' => 'require|regex:/.{6}/', 'age' => 'number|between:1,120', 'email' => 'email' ]; protected $message = [ 'name.require' => 'name不能少', 'name.regex' => 'name不能少于6个字符', 'age.number' => 'age必须是数字', 'age.between' => 'age必须在1到120之间', 'email.email' => 'email格式不对', ]; protected $scene = [ 'name_email' => ['name','email'], ]; } ?>
Use
<?php namespace app\home\controller; use think\Loader; use think\Controller; class Index extends Controller { public function test(){ $date = [ 'name'=>'qw2e', 'email'=>'12313' ]; //$validate = Loader::validate('Test');//使用加载类Loader $validate = validate('Test');//使用助手函数 $result = $validate->scene('name_email')->check($date); if(!$result){ dump($validate->getError()); } } }
in the Index controller test method. Related recommendations:
thinkPHP5.0 framework application request life cycle analysis
thinkPHP5.0 framework independent configuration and dynamic configuration methods
The above is the detailed content of How to use thinkphp5.0 verification class. For more information, please follow other related articles on the PHP Chinese website!