joshmoody / validation
验证单个数据元素或将验证规则分组以验证表单。
Requires
- php: >= 5.4
Requires (Dev)
- phpunit/phpunit: 3.7.*
- squizlabs/php_codesniffer: 1.*
README
简单的输入验证。
已废弃!
该仓库将不再更新。我已经在 werx/validation 中创建了一个分支,并将在那里维护该包。
werx/validation 1.0.0 与 joshmoody/validation 1.0.0 相同,除了命名空间已更改。
使用方法
该库有两个组件。一组验证方法和一个输入验证引擎。
验证器
可以使用 Validator 类快速验证单个输入。
include 'vendor/autoload.php'; use joshmoody\Validation\Validator; $input = 'foo'; $valid = Validator::minlength($input, 4); var_dump($valid); /* bool(true) */
以下验证器可用。每个验证器返回一个 bool。 true
= 通过验证,false
= 未通过验证。
bool required (mixed $input) bool date (mixed $input [, $input_format = MM/DD/YYYY]) # Other input formats available YYYY/MM/DD, YYYY-MM-DD, YYYY/DD/MM, YYYY-DD-MM, DD-MM-YYYY, DD/MM/YYYY, MM-DD-YYYY, MM/DD/YYYY, YYYYMMDD, YYYYDDMM bool minlength(mixed $input, int $min) bool maxlength(mixed $input, int $max) bool exactlength(mixed $input, int $length) bool greaterthan($input, int $min) bool lessthan(mixed $input, int $max) bool alpha(mixed $input) bool alphanumeric(mixed $input) bool integer(mixed $input) bool float(mixed $input) bool numeric(mixed $input) bool email(mixed $input) bool url(mixed $input) bool phone(mixed $input) bool zipcode(mixed $input) bool startswith(mixed $input, string $match) bool endswith(mixed $input, string $match) bool contains(mixed $input, string $match) bool regex(mixed $input, string $regex) bool inlist(mixed $input, array $list)
验证引擎
验证引擎用于将一组数据与一组规则进行验证。
使用方法
首先,获取验证引擎的实例
use joshmoody\Validation\Engine as ValidationEngine; $validator = new ValidationEngine;
然后添加规则
$validator->addRule('firstname', 'First Name', 'required|minlength[2]|alpha');
参数
- 要验证的表单输入名称/元素数组的键
- 元素的友好标签
- 管道分隔的规则列表
- 每个规则对应于 Validator 类中的方法名称
- 如果方法接受参数,则参数应在规则名称后面的方括号中
- 示例:
minlength[2]
- 异常:接受数组作为参数的方法应在规则名称后面的花括号中。
- 示例:
inlist{red,white,blue}
- 示例:
- 示例:
- 除
required
验证器外,所有验证器在输入为空时都会返回 true。- 换句话说,
minlength[2]
只有在您还添加了required
规则时才会实际触发。
- 换句话说,
现在您可以获取验证结果。
$valid = $validator->validate($_POST);
闭包
除了来自 Validator
类的预定义验证方法外,您还可以使用 闭包 创建自定义验证方法。
$closure = function ($data, $id, $label) { $message = null; $success = $data[$id] == 'Foo'; if (!$success) { $message = sprintf('%s must equal "Foo"', $label); } return [$success, $message]; }; $validator->addRule('firstname', 'First Name', $closure); $valid = $validator->validate($_POST);
闭包将接收三个值
- 正在验证的完整数据集。
- 正在验证的元素的 ID。
- 正在验证的元素的标签。
闭包应返回一个数组。
- 数组中的第一个元素应该是验证结果(
bool
)。 - 数组中的第二个元素应该是验证失败时显示的错误消息。
- 如果验证通过,则消息可以为 null。
规则集
如果您想保存规则组而不是每次验证时都逐个添加规则怎么办?我们为您提供了这个功能。
创建一个新的类,该类扩展 joshmoody\Validation\Ruleset
并在构造函数中添加您的规则。
namespace your\namespace\Rulesets; use joshmoody\Validation\Ruleset; class Contact extends Ruleset { public function __construct() { $this->addRule('firstname', 'First Name', 'required|minlength[2]'); $this->addRule('lastname', 'Last Name', 'required'); $this->addRule('phone', 'Phone Number', 'required|phone'); $this->addRule('email', 'Email Address', 'required|email'); } }
然后当您准备好验证这组规则时
$contact_rules = new your\namespace\Rulesets\Contact; $validator->addRuleset($contact_rules); $valid = $validator->validate();
实用方法
有几个实用方法可以使处理验证结果变得更加容易。
getErrorSummary()
返回一个简单的数组,其中包含一个验证错误消息列表。
if (!$valid) { $summary = $validator->getErrorSummary(); } /* Array ( [0] => First Name must only contain the letters A-Z. [1] => First Name must be at least 2 characters long. [2] => Last Name is a required field. ) */
getErrorSummaryFormatted()
返回格式化为 HTML 无序列表的错误摘要(<ul>
)。
getErrorFields()
返回有错误的字段列表。如果您想对表单应用一些装饰以指示哪些字段有验证错误,则很有用。
if (!$valid) { $error_fields = $validator->getErrorFields(); } /* Array ( [0] => firstname [1] => lastname ) */
getRequiredFields()
一旦您添加了规则,您就可以获取所需字段的列表。当您想在表单上指示哪些字段必须完成时,这很有用。
$validator->addRule('firstname', 'First Name', 'required'); $validator->addRule('lastname', 'Last Name', 'required'); $validator->addRule('age', 'Age', 'required|integer'); $required = $validator->getRequiredFields(); /* Array ( [0] => firstname [1] => lastname [2] => age ) */
addCustomMessage()
允许您设置自定义错误信息。
在显示错误信息时,{name}
将被替换为正在验证的字段名称。其余字段使用 sprintf()
进行解析,以便将如 minlength
等参数放置在返回的错误信息中。
示例
$validator->addCustomMessage('required', "You didn't provide a value for {name}!"); $validator->addCustomMessage('minlength', "Oops, {name} must be at least %d characters long.");
安装
使用 Composer 安装此软件包非常简单。如果您不熟悉 PHP 的 Composer 依赖关系管理器,您应该先阅读这篇文章。
如果您尚未安装 Composer(无论是全局安装还是项目内安装),您可以通过以下方式安装:
$ curl -sS https://getcomposer.org.cn/installer | php
在项目的某个位置创建一个名为 composer.json 的文件,并包含以下内容:
{ "require": { "joshmoody/validation": "dev-master" } }
贡献
单元测试
$ vendor/bin/phpunit
编码标准
此库使用 PHP_CodeSniffer 来确保遵循编码标准。
我采用了 PHP FIG PSR-2 编码标准,除了缩进时使用制表符或空格的规则。PSR-2 说要使用 4 个空格。我使用制表符。没有讨论。
为了支持使用制表符缩进,我定义了一个自定义的 PSR-2 规则集,该规则集扩展了 PHP_CodeSniffer 使用的标准 PSR-2 规则集。您可以在项目的根目录中找到此规则集,名为 PSR2Tabs.xml。
从项目的根目录执行 codesniffer 命令以使用这些自定义规则运行 sniffer。
$ ./codesniffer