## 簡介
### 特點
* 語意明確,易于理解。采用\*, >, =, len>, int, (n), (s) 等函數標志,比如(n)表示in\_array, 且必須是數字
* 支持正則表達式驗證
* 支持條件驗證,條件滿足則繼續驗證后續規則,不滿足則表明該字段是可選擇
* 支持串聯(一個參數多個規則同時滿足, &&),并聯(一個參數多個規則滿足其一, ||)驗證
* 支持自定義函數驗證
* 支持函數驗證時自由傳參,@root(原數據), @parent(驗證字段的父數據), @me(當前字段),@anything(任意字段)
* 支持無限嵌套的數據結構的驗證,包括關聯數組,索引數組
* 支持特殊的驗證規則
* 支持自定義配置,比如規則分隔符號"|",參數分隔符號","等等
* 支持國際化配置,默認英語,支持自定義方法返回錯誤信息
* 支持一次性驗證所有參數(默認),也可設置參數驗證失敗后立即結束驗證
* 支持自定義錯誤信息,支持多種格式的錯誤信息,無限嵌套或者一維數組的錯誤信息格式
* ~暫時想不到,想到了再給你們編。~
## 2\. 安裝
> composer require githusband/validation
## 3\. 完整示例
設想一下,如果用戶數據如下,它包含關聯數組,索引數組,我們要如何定制規則去驗證它,如何做到簡單直觀呢?
~~~php
$data = [
"id" => "",
"name" => "12",
"email" => "10000@qq.com.123@qq",
"phone" => "15620004000-",
"education" => [
"primary_school" => "???Qiankeng Xiaoxue",
"junior_middle_school" => "Foshan Zhongxue",
"high_school" => "Mianhu Gaozhong",
"university" => "Foshan",
],
"company" => [
"name" => "Qianken",
"website" => "https://www.qiankeng.com1",
"colleagues" => [
[
"name" => 1,
"position" => "Reception"
],
[
"name" => 2,
"position" => "Financial1"
],
[
"name" => 3,
"position" => "JAVA"
],
[
"name" => "Kurt",
"position" => "PHP1"
],
],
"boss" => [
"Mike1",
"David",
"Johnny2",
"Extra",
]
],
"favourite_food" => [
[
"name" => "HuoGuo",
"place_name" => "SiChuan"
],
[
"name" => "Beijing Kaoya",
"place_name" => "Beijing"
],
]
];
~~~
~~~php
// $data - 上述待驗證的數據
function validate($data) {
// 設置驗證規則
$rule = [
// id 是必要的且必須匹配正則 /^\d+$/, >> 后面的required 和 正則對應的報錯信息
"id" => 'required|/^\d+$/ >> { "required": "用戶自定義 - @me 是必要的", "preg": "用戶自定義 - @me 必須匹配 @preg" }',
// name 是必要的且必須是字符串且長度在區間 【8,32)
"name" => "required|string|len<=>:8,32",
"email" => "required|email",
"phone" => "required|/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/ >> 用戶自定義 - phone number 錯誤",
// ip 是可選的
"ip" => "optional|ip",
"education" => [
// education.primary_school 必須等于 “Qiankeng Xiaoxue”
"primary_school" => "required|=:Qiankeng Xiaoxue",
"junior_middle_school" => "required|!=:Foshan Zhongxue",
"high_school" => "optional|string",
"university" => "optional|string",
],
"company" => [
"name" => "required|len<=>:8,64",
"website" => "required|url",
"colleagues.*" => [
"name" => "required|string|len<=>:3,32",
// company.colleagues.*.position 必須等于 Reception,Financial,PHP,JAVA 其中之一
"position" => "required|(s):Reception,Financial,PHP,JAVA"
],
// 以下三個規則只對 boss.0, boss.1, boss.2 有效,boss.3 及其他都無效
"boss" => [
"required|=:Mike",
"required|(s):Johnny,David",
"optional|(s):Johnny,David"
]
],
// favourite_food 是可選的索引數組,允許為空
"favourite_food[optional].*" => [
// favourite_food.*.name 必須是字符串
"name" => "required|string",
"place_name" => "optional|string"
]
];
// 簡單的自定義配置,它還不是完整的,也不是必要的
$validation_conf = [
'language' => 'zh-cn',
'validation_global' => true,
];
// 實例化類,不要忘了事先引用類文件
// 接受一個配置數組,但不必要
$validation = new Validation($validation_conf);
// 設置驗證規則并驗證數據
if($validation->set_rules($rule)->validate($data)) {
// 這里獲取檢測結果,有驗證到的參數,成功則修改其值為true,失敗則修改其值為錯誤信息,
// 如無驗證到的參數,保持原值不變。
return $validation->get_result();
}else {
// 這里有兩個參數,分別對應不同的錯誤信息格式,一共有四種錯誤信息可供選擇。
return $validation->get_error(true, false);
}
}
// 可以通過改變get_error 的兩個參數,找到適合自己的報錯格式
// 例子中的 $data 基本都不滿足 $rule ,可以改變 $data 的值,驗證規則是否正確
print_r(validate($data));
~~~
打印結果為
~~~php
Array
(
[id] => 用戶自定義 - id 是必要的
[name] => name 長度必須大于 8 且小于等于 32
[email] => email 必須是郵箱
[phone] => 用戶自定義 - phone number 錯誤
[education.primary_school] => education.primary_school 必須等于 Qiankeng Xiaoxue
[education.junior_middle_school] => education.junior_middle_school 必須不等于 Foshan Zhongxue
[company.name] => company.name 長度必須大于 8 且小于等于 64
[company.website] => company.website 必須是網址
[company.colleagues.0.name] => company.colleagues.0.name 必須是字符串
[company.colleagues.1.name] => company.colleagues.1.name 必須是字符串
[company.colleagues.1.position] => company.colleagues.1.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA
[company.colleagues.2.name] => company.colleagues.2.name 必須是字符串
[company.colleagues.3.position] => company.colleagues.3.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA
[company.boss.0] => company.boss.0 必須等于 Mike
[company.boss.2] => company.boss.2 必須是字符串且在此之內 Johnny,David
)
~~~
理論上,該工具是用于驗證復雜的數據結構的,但如果你想驗證單一字符串,也可以,例如
~~~php
$validation->set_rules("required|string")->validate("Hello World!");
~~~
**注意**:在 src/Test 目錄下已經內置了完整測試類Tests.php 和單元測試類 Unit.php。
**完整測試類**:
~~~cpp
// 上述測試代碼
php Tests.php readme_case
// 驗證成功測試, 返回測試結果:
php Tests.php success
// 驗證成功測試,返回錯誤信息:
php Tests.php error
~~~
**單元測試類**:
包含了所有功能的測試,只包含部分內置函數
原則上修改代碼后跑一遍單元測試,確保功能正常。如果測試報錯,定位問題再解決。
~~~kotlin
// 測試所有例子:
php Unit.php run
// 測試單一例子,如測試正則表達式:
php Unit.php run test_regular_expression
~~~
## 4\. 功能介紹
### 4.1 語意明確
為了便于理解以及使規則更加簡短,允許采用一些函數***標志***代表實際的功能。
~~~php
// 名字是必要的,必須是字符串,長度必須大于3且小于等于32
"name" => "required|string|length_greater_lessequal:3,32"
// 采用函數標志,同上
// 若是覺得函數標志難以理解,請直接使用函數全稱即可
"name" => "required|string|len<=>:3,32"
~~~
例如:
| 標志 | 函數 | 含義 |
| --- | --- | --- |
| \* | required | 必要的,不允許為空 |
| O | optional | 可選的,允許不設置或為空 |
| O! | unset\_required | 可選的,允許不設置,一旦設置則不能為空 |
| \>:20 | greater\_than | 數字必須大于20 |
| len:2,16 | length\_greater\_lessequal | 字符長度必須大于2且小于等于16 |
| ip | ip | 必須是ip地址 |
**完整功能請查看附錄1**
### 4.2 支持正則表達式驗證
以 "**/**" 開頭,以 "**/**" 結尾,表示是正則表達式
~~~php
// id 必須是數字
"id" => "required|/^\d+$/",
~~~
### 4.3 條件驗證
條件驗證標志是 "**if\[01\]?\\?**"
正條件:**if?** 和 **if1?**
* 如果條件成立,則繼續驗證后續規則
* 如果條件不成立,說明該字段是可選的:1. 若該字段為空,立刻返回驗證成功;2. 若該字段不為空,則繼續驗證后續規則
~~~php
$rule = [
"gender" => "required|(s):male,female",
// 若性別是女性,則要求年齡大于22歲,若為男性,則對年齡無要求
"age" => "if?=::@gender,female|required|>:22",
],
~~~
否條件:**if0?**
* 如果條件不成立,則繼續驗證后續規則
* 如果條件成立,說明該字段是可選的:1. 若該字段為空,立刻返回驗證成功;2. 若該字段不為空,則繼續驗證后續規則
~~~php
$rule = [
"gender" => "required|(s):male,female",
// 若性別不是女性,則要求年齡大于22歲,若為女性,則對年齡無要求
"age" => "if0?=::@gender,female|required|>:22",
],
~~~
### 4.4 支持串聯,并聯驗證
* 串聯:一個參數多個規則同時滿足,標志是 **|**
* 并聯:一個參數多個規則滿足其一,標志是 {字段名} + **\[or\]**
~~~php
// 并聯
"height[or]" => [
// 若身高單位是cm, 則身高必須大于等于100,小于等于200
"required|=::@height_unit,cm|<=>=:100,200 >> @me should be in [100,200] when height_unit is cm",
// 若身高單位是m, 則身高必須大于等于1,小于等于2
"required|=::@height_unit,m|<=>=:1,2 >> @me should be in [1,2] when height_unit is m",
],
// 串聯,身高單位是必須的,且必須是 cm 或者 m
"height_unit" => "required|(s):cm,m",
~~~
### 4.5 自定義函數驗證
本工具已經內置了不少驗證函數,例如 \*,>, len>=, ip 等等
如果內置函數無法滿足需求,或者驗證規則過于復雜,可以使用自定義函數驗證
自定義函數驗證支持兩種方式:
* 自定義接口:add\_method
~~~php
$validation->add_method('check_postcode', function($company) {
if(isset($company['country']) && $company['country'] == "US"){
if(!isset($company['postcode']) || $company['postcode'] != "123"){
return false;
}
}
return true;
});
~~~
* 全局函數
三種函數的優先級是
> add\_method > 內置函數 > 全局函數
如若函數不存在,則報錯。
使用函數允許自由傳參,每一個參數以 "**,**" 分隔
| 參數 | 含義 |
| --- | --- |
| @root | 代表參數是整個驗證數據$data |
| @parent | 代表參數是當前字段的父元素 |
| @me | 代表參數是當前字段 |
| @field\_name | 代表參數是整個驗證數據中的字段名是 field\_name的字段 |
| value | 代表參數是value 字符串,允許為空 |
參數的標志是 ":" 和 "::"
> ":" 這個標志,如果不存在@me, 會自動添加 @me 參數到第一個參數
> "::" 這個標志,如果不存在@me, 也不會自動添加
### 4.6 無限嵌套的數據結構的驗證
支持無限嵌套的數據結構的驗證,包括關聯數組,索引數組, 例如:
~~~php
$data = [
"name" => "Johnny",
"favourite_color" => {
"white",
"red"
},
"favourite_fruits" => {
[
"name" => "apple",
"color" => "red",
"shape" => "circular"
],
[
"name" => "banana",
"color" => "yellow",
"shape" => "long strip"
],
}
]
// 若要驗證上述 $data,規則可以這么寫
$rule = [
"name" => "required|len>:4",
"favourite_color" => [
"required|len>:4",
"required|len>:4",
],
"favourite_fruits.*" => [
"name" => "required|len>:4",
"color" => "required|len>:4",
"shape" => "required|len>:4"
]
]
~~~
**發現了嗎?**
關聯數組和普通索引數組正常寫就可以了,而索引數組里的元素是關聯數組,則需要在字段后面加上 "**.\***" 這個標志即可。
**可選數組規則**
有時候,數組也是可選的,但是一旦設置,其中的子元素必須按規則驗證,這時候只需要在數組字段名后面加上"**\[optional\]**" 標志,表示該數組可選,如:
~~~dart
"favourite_fruits[optional].*" => [
"name" => "required|len>:4",
"color" => "required|len>:4",
"shape" => "required|len>:4"
]
~~~
### 4.7 支持特殊的驗證規則
支持的特殊規則有:
"**\[optional\]**" - 表明單個字段或數組是可選的。
注意,表明單個字段可選,可在字段規則上加上 **optional** 即可。
~~~php
$rule = [
"name" => "optional|string",
"gender" => [ "[optional]" => "string" ],
// favourite_fruit 是可選的,如果存在,則必須是數組
"favourite_fruit[optional]" => [
"name" => "required|string",
"color" => "required|string"
],
// 等同于上的寫法
"favourite_meat" => [
"[optional]" => [
"name" => "required|string",
"from" => "required|string"
]
],
];
~~~
"**\[or\]**" - 表明單個字段是或規則,多個規則滿足其一即可。
~~~php
$rule = [
// name 可以是布爾值或者布爾字符串
"name[or]" => [
"required|bool",
"required|bool_str",
],
// 等同于上的寫法
"height" => [
"[or]" => [
"required|int|>:100",
"required|string",
]
]
];
~~~
"**.\***" - 表明該字段是索引數組。
當索引數組的標志以 . 開頭時,在標志不是跟隨在字段名后面的情況下,可省略 .
~~~php
$rule = [
"person" => [
// 表明 person 是索引數組, person.* 是關聯數組
// 在這種情況下,可省略 . ,只寫 *
"*" => [
"name" => "required|string",
// 表明 person.*.relation 是關聯數組
"relation" => [
"father" => "required|string",
"mother" => "optional|string",
"brother" => [
// 表明 person.*.relation.*.brother 是可選的索引數組
"[optional].*" => [
// 表明 person.*.relation.*.brother.* 是索引數組
"*" => [
"name" => "required|string",
"level" => [
"[or]" => [
"required|int",
"required|string",
]
]
]
]
]
],
"fruit" => [
"*" => [
"*" => [
"name" => "required|string",
"color" => "optional|string",
]
]
],
]
],
];
// 驗證數據格式如下
$data = [
"person" => [
[
"name" => "Devin",
"relation" => [
"father" => "fDevin",
"mother" => "mDevin",
"brother" => [
[
["name" => "Tom", "level" => 1],
["name" => "Mike", "level" => "Second"],
]
]
],
"fruit" => [
[
["name" => "Apple", "color" => "Red"],
["name" => "Banana", "color" => "Yellow"],
],
[
["name" => "Cherry", "color" => "Red"],
["name" => "Orange", "color" => "Yellow"],
]
]
],
[
"name" => "Johnny",
"relation" => ["father" => "fJohnny", "mother" => "mJohnny"],
"fruit" => [
[
["name" => "Apple", "color" => "Red"],
["name" => "Banana", "color" => "Yellow"],
],
[
["name" => "Cherry", "color" => "Red"],
["name" => "Orange", "color" => "Yellow"],
]
]
],
],
]
~~~
### 4.8 支持自定義配置
支持自定義的配置有:
~~~php
$config = array(
'language' => 'en-us', // Language, default is en-us
'lang_path' => '', // Customer Language file path
'validation_global' => true, // If true, validate all rules; If false, stop validating when one rule was invalid
'auto_field' => "data", // If root data is string or numberic array, add the auto_field to the root data, can validate these kind of data type.
'reg_msg' => '/ >> (.*)$/', // Set special error msg by user
'reg_preg' => '/^(\/.+\/.*)$/', // If match this, using regular expression instead of method
'reg_if' => '/^if[01]?\?/', // If match this, validate this condition first
'reg_if_true' => '/^if1?\?/', // If match this, validate this condition first, if true, then validate the field
'reg_if_false' => '/^if0\?/', // If match this, validate this condition first, if false, then validate the field
'symbol_rule_separator' => '|', // Rule reqarator for one field
'symbol_param_classic' => ':', // If set function by this symbol, will add a @me parameter at first
'symbol_param_force' => '::', // If set function by this symbol, will not add a @me parameter at first
'symbol_param_separator' => ',', // Parameters separator, such as @me,@field1,@field2
'symbol_field_name_separator' => '.', // Field name separator, suce as "fruit.apple"
'symbol_required' => '*', // Symbol of required field, Same as "required"
'symbol_optional' => 'O', // Symbol of optional field, can be unset or empty, Same as "optional"
'symbol_unset_required' => 'O!', // Symbol of optional field, can only be unset or not empty, Same as "unset_required"
'symbol_or' => '[||]', // Symbol of or rule, Same as "[or]"
'symbol_array_optional' => '[O]', // Symbol of array optional rule, Same as "[optional]"
'symbol_index_array' => '.*', // Symbol of index array rule
);
~~~
例如:
~~~php
$validation_conf = array(
'language' => 'en-us', // Language, default is en-us
'lang_path' => '/my_path/', // Customer Language file path
'validation_global' => true, // If true, validate all rules; If false, stop validating when one rule was invalid.
'auto_field' => "param", // If root data is string or numberic array, add the auto_field to the root data, can validate these kind of data type.
'reg_msg' => '/ >>>(.*)$/', // Set special error msg by user
'reg_preg' => '/^Reg:(\/.+\/.*)$/', // If match this, using regular expression instead of method
'reg_if' => '/^IF[yn]?\?/', // If match this, validate this condition first
'reg_if_true' => '/^IFy?\?/', // If match this, validate this condition first, if true, then validate the field
'reg_if_false' => '/^IFn\?/', // If match this, validate this condition first, if false, then validate the field
'symbol_rule_separator' => '&&', // Rule reqarator for one field
'symbol_param_classic' => '~', // If set function by this symbol, will add a @me parameter at first
'symbol_param_force' => '~~', // If set function by this symbol, will not add a @me parameter at first
'symbol_param_separator' => ',', // Parameters separator, such as @me,@field1,@field2
'symbol_field_name_separator' => '->', // Field name separator, suce as "fruit.apple"
'symbol_required' => '!*', // Symbol of required field, Same as "required"
'symbol_optional' => 'o', // Symbol of optional field, can be unset or empty, Same as "optional"
'symbol_unset' => 'O!', // Symbol of optional field, can only be unset or not empty, Same as "unset_required"
'symbol_or' => '[or]', // Symbol of or rule, Same as "[or]"
'symbol_array_optional' => '[o]', // Symbol of array optional rule, Same as "[optional]"
'symbol_index_array' => '[N]', // Symbol of index array rule
);
~~~
相關規則可以這么寫:
~~~php
$rule = [
"id" => "!*&&int&&Reg:/^\d+$/i",
"name" => "!*&&string&&len<=>~8,32",
"gender" => "!*&&(s)~male,female",
"dob" => "!*&&dob",
"age" => "!*&&check_age~@gender,30 >>>@me is wrong",
"height_unit" => "!*&&(s)~cm,m",
"height[or]" => [
"!*&&=~~@height_unit,cm&&<=>=~100,200 >>>@me should be in [100,200] when height_unit is cm",
"!*&&=~~@height_unit,m&&<=>=~1,2 >>>@me should be in [1,2] when height_unit is m",
],
"education" => [
"primary_school" => "!*&&=~Qiankeng Xiaoxue",
"junior_middle_school" => "!*&&!=~Foshan Zhongxue",
"high_school" => "IF?=~~@junior_middle_school,Mianhu Zhongxue&&!*&&len>~10",
"university" => "IFn?=~~@junior_middle_school,Qiankeng Zhongxue&&!*&&len>~10",
],
"company" => [
"name" => "!*&&len<=>~8,64",
"country" => "o&&len>=~3",
"addr" => "!*&&len>~16",
"colleagues[N]" => [
"name" => "!*&&string&&len<=>~3,32",
"position" => "!*&&(s)~Reception,Financial,PHP,JAVA"
],
"boss" => [
"!*&&=~Mike",
"!*&&(s)~Johnny,David",
"o&&(s)~Johnny,David"
]
],
"favourite_food[o][N]" => [
"name" => "!*&&string",
"place_name" => "o&&string"
]
];
~~~
### 4.9 支持國際化配置
配置文件名和類名都采用大駝峰命名方式。
調用時,支持使用標準的語言代碼,如"zh-cn", "en-us"等。
目前支持的語言有,"zh-cn", "en-us",默認語言是"en-us"英語。
~~~php
// 調用接口
$validation->set_language('zh-cn'); //將加載 ZhCn.php 配置文件
// 或者在實例化類的時候加入配置
$validation_conf = [
'language' => 'zh-cn',
];
$validation = new Validation($validation_conf);
~~~
**自定義國際化文件**
如 /MyPath/MyLang.php。
內容如下:
~~~php
<?php
class MyLang
{
public $error_template = array(
'check_custom' => '@me error!(CustomLang File)'
);
}
~~~
修改語言文件路徑
~~~php
// You should add CustomLang.php in '/MyPath/'
$validation->set_config(array('lang_path' => /MyPath/'))->set_language('MyLang');
~~~
實際上,國際化配置,配置的是每個驗證函數返回的錯誤信息,也可以自由地覆蓋增加你的驗證函數返回的錯誤信息。
~~~php
// 必須是對象
$lang_config = (object)array();
$lang_config->error_template = array(
'check_id' => '@me error!(customed)'
);
$validation->custom_language($lang_config);
~~~
以上為錯誤模版增加了一個check\_id, 如果check\_id 函數驗證錯誤,則返回信息
~~~bash
'@me error!(customed)'
~~~
### 4.10 支持一次性驗證所有參數
支持一次性驗證所有參數(默認),也可設置參數驗證失敗后立即結束驗證
~~~php
// 調用接口
$validation->set_validation_global(false);
// 或者在實例化類的時候加入配置
$validation_conf = [
'validation_global' => false,
];
$validation = new Validation($validation_conf);
~~~
### 4.11 支持自定義錯誤信息
自定義錯誤信息的標志是 " >> ", 注意前后空格。
例如:
1. \*或者**正則**或者=方法 錯誤都報錯 "id is incorrect."
~~~dart
"id" => 'required|/^\d+$/|<=>=:1,100| >> @me is incorrect.'
~~~
2. 支持JSON 格式錯誤信息,為每一個方法設置不同的錯誤信息
~~~csharp
"id" => 'required|/^\d+$/|<=>=:1,100| >> { "required": "Users define - @me is required", "preg": "Users define - @me should be \"MATCHED\" @preg"}'
# 對應的報錯信息為
# id - Users define - id is required
# /^\d+$/ - Users define - id should be \"MATCHED\" /^\d+$/
# <=>= - id must be greater than or equal to 1 and less than or equal to 100
~~~
3. 支持特殊格式錯誤信息,為每一個方法設置不同的錯誤信息,同JSON
~~~csharp
"id" => "required|/^\d+$/|<=>=:1,100| >> [required]=> Users define - @me is required [preg]=> Users define - @me should be \"MATCHED\" @preg"
# 對應的報錯信息為
# id - Users define - id is required
# /^\d+$/ - Users define - id should be \"MATCHED\" /^\d+$/
# <=>= - id must be greater than or equal to 1 and less than or equal to 100
~~~
**自定義函數也可自定義錯誤信息, 優先級低于 " >> "**
當函數返回值 === true 時,表示驗證成功,否則表示驗證失敗
所以函數允許三種錯誤返回:
1. 直接返回 false
2. 返回錯誤信息字符串
3. 返回錯誤信息數組,默認有兩個字段,error\_type 和 message,支持自定義字段
~~~php
function check_age($data, $gender, $param) {
if($gender == "male") {
// if($data > $param) return false;
if($data > $param) return "@me should be greater than @p1 when gender is male";
}else {
if($data < $param) return array(
'error_type' => 'server_error',
'message' => '@me should be less than @p1 when gender is female',
"extra" => "extra message"
);
}
return true;
}
~~~
### 4.12 支持多種錯誤信息格式
如果是驗證一旦錯誤則立即返回的情況下,有兩種錯誤信息格式可以返回:
返回錯誤信息字符串
> $validation->get\_error(false, true);
~~~bash
"id 必須是整型"
~~~
返回錯誤信息數組,默認有兩個字段,error\_type 和 message,支持自定義字段
> $validation->get\_error(false, false);
~~~json
{
"error_type": "validation",
"message": "id 必須是整型"
}
~~~
如果是驗證所有字段的情況下,有兩種錯誤信息格式可以返回:
詳見附錄 2
* * *
## 附錄 1
| 標志 | 函數 | 含義 |
| --- | --- | --- |
| \* | required | 必要的,@me 不能為空 |
| O | optional | 可選的,允許不設置或為空 |
| O! | symbol\_unset\_required | 可選的,允許不設置,一旦設置則不能為空 |
| \= | equal | @me 必須等于 @p1 |
| != | not\_equal | @me 必須不等于 @p1 |
| \== | identically\_equal | @me 必須全等于 @p1 |
| !== | not\_identically\_equal | @me 必須不全等于 @p1 |
| \> | greater\_than | @me 必須大于 @p1 |
| < | less\_than | @me 必須小于 @p1 |
| \>= | greater\_than\_equal | @me 必須大于等于 @p1 |
| <= | less\_than\_equal | @me 必須小于等于 @p1 |
| <> | interval | @me 必須大于 @p1 且小于 @p2 |
| | greater\_lessequal | @me 必須大于 @p1 且小于等于 @p2 |
| <>= | greaterequal\_less | @me 必須大于等于 @p1 且小于 @p2 |
| = | greaterequal\_lessequal | @me 必須大于等于 @p1 且小于等于 @p2 |
| (n) | in\_number | @me 必須是數字且在此之內 @p1 |
| !(n) | not\_in\_number | @me 必須是數字且不在此之內 @p1 |
| (s) | in\_string | @me 必須是字符串且在此之內 @p1 |
| !(s) | not\_in\_string | @me 必須是字符串且不在此之內 @p1 |
| len= | length\_equal | @me 長度必須等于 @p1 |
| len!= | length\_not\_equal | @me 長度必須不等于 @p1 |
| len> | length\_greater\_than | @me 長度必須大于 @p1 |
| len< | length\_less\_than | @me 長度必須小于 @p1 |
| len>= | length\_greater\_than\_equal | @me 長度必須大于等于 @p1 |
| len<= | length\_less\_than\_equal | @me 長度必須小于等于 @p1 |
| len<> | length\_interval | @me 長度必須大于 @p1 且小于 @p2 |
| len | length\_greater\_lessequal | @me 長度必須大于 @p1 且小于等于 @p2 |
| len<>= | length\_greaterequal\_less | @me 長度必須大于等于 @p1 且小于 @p2 |
| len= | length\_greaterequal\_lessequal | @me 長度必須大于等于 @p1 且小于等于 @p2 |
| int | integer | @me 必須是整型 |
| float | float | @me 必須是小數 |
| string | string | @me 必須是字符串 |
| arr | arr | @me 必須是數組, |
| bool | bool | @me 必須是布爾型 |
| bool= | bool | @me 必須是布爾型且等于 @p1 |
| bool\_str | bool\_str | @me 必須是布爾型字符串 |
| bool\_str= | bool\_str | @me 必須是布爾型字符串且等于 @p1 |
| email | email | @me 必須是郵箱 |
| url | url | @me 必須是網址 |
| ip | ip | @me 必須是IP地址 |
| mac | mac | @me 必須是MAC地址 |
| dob | dob | @me 必須是正確的日期 |
| file\_base64 | file\_base64 | @me 必須是正確的文件的base64碼 |
| uuid | uuid | @me 必須是 UUID |
* * *
* * *
## 附錄 2
### 第一種
> $validation->get\_error(false, true);
~~~json
{
"id": "用戶自定義 - id 是必要的",
"name": "name 長度必須大于 8 且小于等于 32",
"email": "email 必須是郵箱",
"phone": "用戶自定義 - phone number 錯誤",
"education.primary_school": "education.primary_school 必須等于 Qiankeng Xiaoxue",
"education.junior_middle_school": "education.junior_middle_school 必須不等于 Foshan Zhongxue",
"company.name": "company.name 長度必須大于 8 且小于等于 64",
"company.website": "company.website 必須是網址",
"company.colleagues.0.name": "company.colleagues.0.name 必須是字符串",
"company.colleagues.1.name": "company.colleagues.1.name 必須是字符串",
"company.colleagues.1.position": "company.colleagues.1.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA",
"company.colleagues.2.name": "company.colleagues.2.name 必須是字符串",
"company.colleagues.3.position": "company.colleagues.3.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA",
"company.boss.0": "company.boss.0 必須等于 Mike",
"company.boss.2": "company.boss.2 必須是字符串且在此之內 Johnny,David"
}
~~~
### 第二種
> $validation->get\_error(false, false);
~~~json
{
"id": {
"error_type": "required_field",
"message": "用戶自定義 - id 是必要的"
},
"name": {
"error_type": "validation",
"message": "name 長度必須大于 8 且小于等于 32"
},
"email": {
"error_type": "validation",
"message": "email 必須是郵箱"
},
"phone": {
"error_type": "validation",
"message": "用戶自定義 - phone number 錯誤"
},
"education.primary_school": {
"error_type": "validation",
"message": "education.primary_school 必須等于 Qiankeng Xiaoxue"
},
"education.junior_middle_school": {
"error_type": "validation",
"message": "education.junior_middle_school 必須不等于 Foshan Zhongxue"
},
"company.name": {
"error_type": "validation",
"message": "company.name 長度必須大于 8 且小于等于 64"
},
"company.website": {
"error_type": "validation",
"message": "company.website 必須是網址"
},
"company.colleagues.0.name": {
"error_type": "validation",
"message": "company.colleagues.0.name 必須是字符串"
},
"company.colleagues.1.name": {
"error_type": "validation",
"message": "company.colleagues.1.name 必須是字符串"
},
"company.colleagues.1.position": {
"error_type": "validation",
"message": "company.colleagues.1.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
},
"company.colleagues.2.name": {
"error_type": "validation",
"message": "company.colleagues.2.name 必須是字符串"
},
"company.colleagues.3.position": {
"error_type": "validation",
"message": "company.colleagues.3.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
},
"company.boss.0": {
"error_type": "validation",
"message": "company.boss.0 必須等于 Mike"
},
"company.boss.2": {
"error_type": "validation",
"message": "company.boss.2 必須是字符串且在此之內 Johnny,David"
}
}
~~~
### 第三種
> $validation->get\_error(true, true);
~~~json
{
"id": "用戶自定義 - id 是必要的",
"name": "name 長度必須大于 8 且小于等于 32",
"email": "email 必須是郵箱",
"phone": "用戶自定義 - phone number 錯誤",
"education": {
"primary_school": "education.primary_school 必須等于 Qiankeng Xiaoxue",
"junior_middle_school": "education.junior_middle_school 必須不等于 Foshan Zhongxue"
},
"company": {
"name": "company.name 長度必須大于 8 且小于等于 64",
"website": "company.website 必須是網址",
"colleagues": [
{
"name": "company.colleagues.0.name 必須是字符串"
},
{
"name": "company.colleagues.1.name 必須是字符串",
"position": "company.colleagues.1.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
},
{
"name": "company.colleagues.2.name 必須是字符串"
},
{
"position": "company.colleagues.3.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
}
],
"boss": {
"0": "company.boss.0 必須等于 Mike",
"2": "company.boss.2 必須是字符串且在此之內 Johnny,David"
}
}
}
~~~
### 第四種
> $validation->get\_error(true, false);
~~~json
{
"id": {
"error_type": "required_field",
"message": "用戶自定義 - id 是必要的"
},
"name": {
"error_type": "validation",
"message": "name 長度必須大于 8 且小于等于 32"
},
"email": {
"error_type": "validation",
"message": "email 必須是郵箱"
},
"phone": {
"error_type": "validation",
"message": "用戶自定義 - phone number 錯誤"
},
"education": {
"primary_school": {
"error_type": "validation",
"message": "education.primary_school 必須等于 Qiankeng Xiaoxue"
},
"junior_middle_school": {
"error_type": "validation",
"message": "education.junior_middle_school 必須不等于 Foshan Zhongxue"
}
},
"company": {
"name": {
"error_type": "validation",
"message": "company.name 長度必須大于 8 且小于等于 64"
},
"website": {
"error_type": "validation",
"message": "company.website 必須是網址"
},
"colleagues": [
{
"name": {
"error_type": "validation",
"message": "company.colleagues.0.name 必須是字符串"
}
},
{
"name": {
"error_type": "validation",
"message": "company.colleagues.1.name 必須是字符串"
},
"position": {
"error_type": "validation",
"message": "company.colleagues.1.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
}
},
{
"name": {
"error_type": "validation",
"message": "company.colleagues.2.name 必須是字符串"
}
},
{
"position": {
"error_type": "validation",
"message": "company.colleagues.3.position 必須是字符串且在此之內 Reception,Financial,PHP,JAVA"
}
}
],
"boss": {
"0": {
"error_type": "validation",
"message": "company.boss.0 必須等于 Mike"
},
"2": {
"error_type": "validation",
"message": "company.boss.2 必須是字符串且在此之內 Johnny,David"
}
}
}
}
~~~