romeoz / rock-validate
PHP 的灵活验证器,支持国际化。
0.12.1
2016-10-18 07:20 UTC
Requires
- php: >=5.4.0
- romeoz/rock-base: ~0.12
- true/punycode: ~1.0
Requires (Dev)
- phpunit/phpunit: ^4.7.0
- romeoz/rock-date: ~0.12
README
特性
- 支持大量验证规则(字符串、数字、ctype、文件、网络)
- 标量变量和数组(
attributes()
)的验证 - 以关联数组形式输出错误列表
- 国际化支持
- 消息占位符的热替换(
{{name}} 必须有效
),以及消息本身 - 自定义验证规则
- Rock 框架模块
粗体特性与 Respect/Validation 不同。
目录
安装
从命令行
composer require romeoz/rock-validate
在你的 composer.json 文件中
{ "require": { "romeoz/rock-validate": "*" } }
快速开始
use rock\validate\Validate; // Validation length from 10 to 20 characters inclusive + regexp pattern $v = Validate::length(10, 20, true)->regex('/^[a-z]+$/i'); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'value must have a length between 10 and 20', 'regex' => 'value contains invalid characters' ] */ $v->getFirstError(); // output: value must have a length between 10 and 20
####替换占位符
use rock\validate\Validate; $v = Validate::length(10, 20, true) ->regex('/^[a-z]+$/i') ->setPlaceholders(['name' => 'username']); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'username must have a length between 10 and 20', 'regex' => 'username contains invalid characters', ] */
####国际化
use rock\validate\Validate; $v = Validate::length(10, 20, true)->regex('/^[a-z]+$/i')->setLocale('ru'); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'значение должно иметь длину в диапазоне от 10 до 20', 'regex' => 'значение содержит неверные символы', ] */
####作为数组或对象
use rock\validate\Validate; $input = [ 'username' => 'O’Reilly', 'email' => 'o-reilly@site' ]; $attributes = [ 'username' => Validate::required() ->length(10, 20, true) ->regex('/^[a-z]+$/i') ->setPlaceholders(['name' => 'username']), 'email' => Validate::required()->email() ]; $v = Validate::attributes($attributes); $v->validate($input); // output false $v->getErrors(); /* output: [ 'username' => [ 'length' => 'username must have a length between 10 and 20', 'regex' => 'username contains invalid characters', ], 'email' => [ 'email' => 'email must be valid email', ] ] */ $attribute = 'email'; $v->getFirstError($attribute); // output: email must be valid
文档
演示
- 安装 Docker 或 askubuntu
docker run --name demo -d -p 8080:80 romeoz/docker-rock-validate
- 打开演示 https://:8080/
要求
- PHP 5.4+
许可证
Rock Validate 是开源软件,使用 MIT 许可证。