Learn how to use thinkphp 5.0 validation classes
- 2021-08-21 19:47:54
- OfStack
Through an example, I will explain the method of verifying classes through thinkphp5.0.
Custom validation class needs to inherit Validate class
For example, create a new validate folder in the home module, and then create a new Test. php authentication class, as follows:
<?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 Can't be less ',
'name.regex' => 'name Can't be less than 6 Characters ',
'age.number' => 'age Must be a number ',
'age.between' => 'age Must be in the 1 To 120 Between ',
'email.email' => 'email Wrong format ',
];
protected $scene = [
'name_email' => ['name','email'],
];
}
?>
Use in Index controller test method
<?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');// Using Load Classes Loader
$validate = validate('Test');// Using helper functions
$result = $validate->scene('name_email')->check($date);
if(!$result){
dump($validate->getError());
}
}
}
The above is the example method of the verification class given by us. If you don't understand anything, you can leave a message below for discussion.