deskola / simple-validator
这是一个从 https://www.phptutorial.net/php-tutorial/ 网站适配的简单验证库。
v1.0.5
2023-06-23 09:48 UTC
Requires
Requires (Dev)
- phpstan/phpstan: ^1.10
README
这是什么?
这是一个用于清理和验证用户输入的 PHP 库。该库基于 https://www.phptutorial.net/ 上的教程,该教程介绍了如何从头开始制作自定义输入验证表单。
- 安装
- 支持的输入类型
- 用法
安装
目前支持 PHP 7.1 到 PHP 8.1 版本。
需要 PECL mbstring 扩展。
建议使用 composer 安装库。
composer require deskola/simple-validator
您也可以使用任何其他 PSR-4 兼容的自动加载器。
如果您不使用 composer,请确保您还加载了此项目依赖的任何依赖项,例如 giggsey/locale。
支持的输入类型
以下是对数据进行清理的数据类型列表。不在列表中的任何数据类型将导致错误
- 字符串
- 整数
- 电子邮件
- 浮点数
- URL
支持的规则
以下是可用于输入的规则
用法
require_once 'vendor/autoload.php'; $validation = new Deskola\SimpleValidator\InputFilter(); $data = [ 'name' => 'Doe', 'email' => 'john@email.com', 'phone' => '2547********' ]; $iso = "KE"; $fields = [ 'name' => "string| required | max: 3", 'email' => 'email| required | email', 'phone' => 'string| required | iso:KE' ]; $response = $validation->filter($data, $fields); print_r($response);
成功输出
Array
(
)
错误输出
Array ( [name] => The name must have at most 3 characters [email] => The email is not a valid email address [phone] => The phone must be a valid phone number )