jedrzej/validator-extended-syntax
Composer 安装命令:
composer require jedrzej/validator-extended-syntax
包简介
Laravel 4/5 package that adds validator negation to Laravel's validation module
关键字:
README 文档
README
This package extends Laravel's validation syntax with the following:
- aliasing validation rule configurations
- negating validation rules
- using automatically replaced placeholders
Composer install
Add the following line to composer.json file in your project:
"jedrzej/validator-extended-syntax": "0.0.3"
or run the following in the commandline in your project's root folder:
composer require "jedrzej/validator-extended-syntax" "0.0.3"
Usage
In order to extend validator syntax, you need to register ValidationServiceProvider in your config/app.php:
'providers' => [ ... /* * Custom proviers */ 'Jedrzej\Validation\ValidationServiceProvider' ];
Aliasing validation rules
It is possible to alias often used rule configuration to allow for reuse. This is an alternative to writing custom validation rules.
// validate if string is a hex calue Validator::alias('hex', 'regex:/^[0-9a-fA-F]+$/'); $rules = [ 'value' => 'hex' ]; $validator = Validator::make($data, $rules); // passing arguments to aliases // validate number is a positive integer no larger than 100 Validator::alias('positive_limited', 'between:1,?'); $rules = [ 'value' => 'positive_limited:100' ]; $validator = Validator::make($data, $rules); // record exists with is_active flag set Validator::alias('active_exists', 'exists:?,?,is_active,1'); $rules = [ 'user_id' => 'active_exists:users,id' ]; $validator = Validator::make($data, $rules);
Negating validation results
When defining validation rules, you can negate chosen rule by prepending its name with exclamation mark. Negated validation rules will fail when not negated rule would pass and vice versa.
$rules = ['string' => 'min:3']; //validate if string is at least 3 characters long $data = ['string' => 'abcde']; $result = Validator::make($data, $rules)->passes(); // TRUE $rules = ['string' => 'min:3']; $data = ['string' => 'ab']; $result = Validator::make($data, $rules)->passes(); // FALSE $rules = ['string' => '!min:3']; $data = ['string' => 'abcde']; $result = Validator::make($data, $rules)->passes(); // FALSE $rules = ['string' => '!min:3']; $data = ['string' => 'ab']; $result = Validator::make($data, $rules)->passes(); // TRUE
Placeholders in validation rules
If validation of one field needs to use the value of another field as parameter, you can use a {{parameter_name}} placeholder in rule definition instead of parameter value.
Value of corresponding field will be passed to validator instead of the placeholder. If the corresponding value is missing in
validated data set, the value will be taken from Config. If it's missing in config, NULL will be used.
$rules = [ 'user_id' => 'exists:users,id' 'email' => 'unique:users,email,{{user_id}}', 'age' => 'min:{{app.min_age}} ];
jedrzej/validator-extended-syntax 适用场景与选型建议
jedrzej/validator-extended-syntax 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.46k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jedrzej/validator-extended-syntax 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jedrzej/validator-extended-syntax 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jedrzej/validator-extended-syntax 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
A Laravel validator for delimiter-separated list of emails.
A CakePHP behavior to validate foreign keys
PHP Validator for stuff.
Stand-alone PHP Class for Data Sanitization and Validation
统计信息
- 总下载量: 11.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-10