nabeelalihashmi / lightvalidator
轻量级且可扩展的PHP验证器。
dev-master
2022-06-23 16:10 UTC
This package is auto-updated.
Last update: 2024-09-23 21:03:04 UTC
README
LightValidator
快速且易于使用的PHP验证器。这很容易扩展。
特性
* Fast
* Easy
* Lightweight
* Supports custom messages
安装
composer require nabeelalihashmi/LightValidator
基本用法
LValidator 需要三个参数,第一个是要验证的数组,第二个是规则,第三个是覆盖的消息。
如果没有提供覆盖的消息,将使用默认消息。
validate
方法在验证成功时返回 true,在验证失败时返回包含错误信息的数组。
$valid_ids = [1,3,4];
$rules = [
'username' => [
'required',
'alphanumeric',
'min' => 3,
'max' => 10
],
'email' => [
'required',
'email'
],
'id' => [
'required',
'numeric',
'in' => $valid_ids
]
];
$overrride_messages = [
'id' => [
'in' => ['ID Must have value betweeen ' . implode(', ', $valid_ids)]
],
'username' => [
'required' => 'Username is required',
'string' => 'Username must be string',
'min' => 'Username must be at least 3 characters',
'max' => 'Username must be at most 10 characters',
],
'email' => [
'required' => 'Email is required',
],
'password' => [
'required' => 'Password is required',
'min' => 'Password must be at least 6 characters',
'max' => 'Password must be at most 10 characters',
],
];
$v = new LValidator($_GET + $_POST, $rules, $overrride_messages);
$results = $v->validate();
var_dump($results);
规则
规则是数组。每个规则都是一个键为字段名、值为规则的数组。规则可以是字符串或键值对。
'username' => [
'required',
'alphanumeric',
'min' => 3,
'max' => 10
]
覆盖消息
覆盖消息是一组消息。键是规则名,值是消息。当规则失败时,将使用此消息。
包中可用的验证
- required($key)
- min($key, $value)
- max($key, $value)
- email($key)
- url($key)
- integer($key)
- float($key)
- boolean($key)
- alpha($key)
- alphanumeric($key)
- numeric($key)
- regex($key, $value)
- equal($key, $value)
- different($key, $value)
- contains($key, $value)
- starts_with($key, $value)
- ends_with($key, $value)
- length($key, $value)
- min_length($key, $value)
- max_length($key, $value)
- in($key, $value)
- not_in($key, $value)
- ip($key)
- mac_address($key)
- date_format($key, $value)
- boolean_string($key)
- is_valid_person_name($key)
- is_valid_person_name_with_space($key)
- date_between($key, $value)
扩展
您可以通过添加规则处理程序来添加自己的规则。规则处理程序是一个具有两个参数的函数,第一个是键,第二个是值。如果值有效,则函数应返回 true;如果值无效,则返回 false。第二个参数是可选的。
public function islessthan10($key) {
return intval($key) < 10;
}
public function islessthanVal($key, $val) { return intval($key) < intval($val); }
------
## License
LightValidator is released under permissive licese with following conditions:
* It cannot be used to create adult apps.
* It cannot be used to gambling apps.
* It cannot be used to create apps having hate speech.
### MIT License
Copyright 2022 Nabeel Ali | IconicCodes.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.