1. Go to this page and download the library: Download githusband/validation 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/ */
githusband / validation example snippets
use githusband\Validation;
// 待验证参数的简单示例。实际上无论参数多么复杂,都支持一个验证规则数组完成验证
$data = [
"id" => 1,
"name" => "Devin",
"age" => 18,
"favorite_animation" => [
"name" => "A Record of A Mortal's Journey to Immortality",
"release_date" => "July 25, 2020 (China)"
]
];
// 验证规则数组。规则数组的格式与待验证参数的格式相同。
$rule = [
"id" => "��但不必要
$validation = new Validation($config);
// 设置验证规则并验证数据,成功返回 true,失败返回 false
if ($validation->set_rules($rule)->validate($data)) {
// 这里获取验证结果,有被规则{$rule}验证到的参数,成功则修改其值为true,失败则修改其值为错误信息,
// 没有被验证到的参数,保持原值不变。比如 age 保持 18 不变。
return $validation->get_result();
} else {
// 一共有四种错误信息格式可供选择。默认 Validation::ERROR_FORMAT_DOTTED_GENERAL
return $validation->get_error();
}
$config = [
'language' => 'en-us', // Language, Default en-us
'lang_path' => '', // Customer Language file path
'enable_entity' => false, // Pre-parse ruleset into ruleset entity to reuse the ruleset without re-parse
'validation_global' => true, // 1. true - validate all rules even though previous rule had been failed; 2. false - stop validating when any rule is failed
'auto_field' => "data", // If root data is string or numberic array, add the auto_field as the root data field name
'reg_msg' => '/ >> (.*)$/sm', // Set the error message format for all the methods after a rule string
'reg_preg' => '/^(\/.+\/.*)$/', // If a rule match reg_preg, indicates it's a regular expression instead of method
'reg_preg_strict' => '/^(\/.+\/[imsxADSUXJun]*)$/', // Verify if a regular expression is valid
'symbol_if' => 'if', // The start of IF construct. e.g. `if ( expr ) { statement }`
'symbol_else' => 'else', // The else part of IF construct. e.g. `else { statement }`. Then the elseif part is `else if ( expr ) { statement }`
'symbol_logical_operator_not' => '!', // The logical operator not. e.g. `if ( !expr ) { statement }`
'symbol_logical_operator_or' => '||', // The logical operator or. e.g. `if ( expr || expr ) { statement }`
'symbol_rule_separator' => '|', // Serial rules seqarator to split a rule into multiple methods
'symbol_parallel_rule' => '[||]', // Symbol of the parallel rule, Same as "[or]"
'symbol_method_standard' => '/^([^\\(]*)\\((.*)\\)$/', // Standard method format, e.g. equal(@this,1)
'symbol_method_omit_this' => '/^([^\\[]*)\\[(.*)\\]$/', // @this omitted method format, will add a @this parameter at first. e.g. equal[1]
'symbol_parameter_separator' => ',', // Parameters separator to split the parameter string of a method into multiple parameters, e.g. equal(@this,1)
'symbol_field_name_separator' => '.', // Field name separator of error message, e.g. "fruit.apple"
'symbol_
$custom_config = [
'reg_preg' => '/^Reg:(\/.+\/.*)$/', // If a rule match reg_preg, indicates it's a regular expression instead of method
'symbol_rule_separator' => '&&', // Serial rules seqarator to split a rule into multiple methods
'symbol_method_standard' => '/^(.*)#(.*)$/', // Standard method format, e.g. equal(@this,1)
'symbol_method_omit_this' => '/^(.*)~(.*)$/', // @this omitted method format, will add a @this parameter at first. e.g. equal[1]
'symbol_parameter_separator' => '+', // Parameters separator to split the parameter string of a method into multiple parameters, e.g. equal(@this,1)
'symbol_field_name_separator' => '->', // Field name separator of error message, e.g. "fruit.apple"
'symbol_