PHP code example of verdient / validator
1. Go to this page and download the library: Download verdient/validator 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/ */
verdient / validator example snippets
use Verdient\Validator\Validation;
/**
* 限定条件,格式为数组
* 第一级的Key为字段的名称,value为索引数组,数组内的每一个数组代表一个校验规则
* 校验规则格式为:第一个元素为校验器的名字,其后的参数为注入到校验器内的参数
*/
$constraints = [
'mobile' => [
['mobile'], //规则1,要求必须是手机号码
['string'], //规则2,要求必须是字符串
],
'date' => [
['date', 'min' => '2020-02-02', 'max' => '2020-02-02'] //校验是否为日期,且最大最小日期均为2020-02-02
],
'in' => [
['in', 'range' => [1, 2, 3]] //校验是否为1, 2, 3中的一个(或多个)
], ...
];
/**
* 要校验的数据
*/
$data = [
'mobile' => '15757116316',
'date' => '2020-02-02',
'in' => 1, ...
];
$result = $validation->validate($data); //返回结果为true或false
use Verdient\Validator\Validators\In;
$validator = new In(['range' => [1, 2, 3]]);
$result = $validator->validate(4);
$errors = $validation->getErrors();
$firstError = $errors->first(); //返回的结果为数组,key为字段名称,value为提示信息
$message = (string) $errors;
$when = ['{name}', '{operator}', '{comparedValue}'];