PHP code example of bang / validate
1. Go to this page and download the library: Download bang/validate library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bang / validate example snippets
namespace app\Validate;
use bang\Validate\Validate;
class User extends Validate {
protected $rule = [
'name' => 'equire' => '名称必须',
'name.max' => '名称最多不能超过25个字符',
'age.number' => '年龄必须是数字',
'age.between' => '年龄只能在1-120之间',
'email' => '邮箱格式错误',
];
protected $scene = [
'add' => ['name','email'],
'edit' => ['name','age'],
];
}
//待验证的数据
$data = [
'name' => 'thinkphp',
'email' => 'thinkphp',
'age' => '[email protected] ',
];
$validate = new User();
if (!$validate->check($data)) {
//打印错误信息
var_dump($validate->getError());
}
//如果要验证场景,则需要
if(!$validate->scene('add')->check($data)){
//打印错误信息
var_dump($validate->getError());
}
$data = [
'name' => 'thinkphp',
'email' => 'thinkphp',
'age' => '[email protected] ',
];
//实例化验证器
$validateNew = new bang\validate\Validate();
$validateNew->rule([
'name' => '
array
'info'=>'array'
date
'num'=>'in:1,2,3'
'num'=>'notIn:1,2,3'
'end_time' => 'before:2016-10-01',
'expire_time' => 'expire:2016-2-1,2016-10-01',
'score'=>'elt:100'
'num'=>'<=:100'
'score'=>'lt:100'
'num'=>'<:100'
file