anlutro/l4-validation
Composer 安装命令:
composer require anlutro/l4-validation
包简介
Validation service class for Laravel.
README 文档
README
Installation: composer require anlutro/l4-validation
Pick the latest stable version from packagist or the GitHub tag list.
WARNING: Backwards compatibility is not guaranteed during version 0.x.
Validation service
This validation class is a layer on top of Laravel's own Validation class (the one you create by calling Validator::make), meant to be injected into a repository or controller. It allows for more advanced rulesets and more dynamic rules, and is being utilized in my repository class.
Your class must implement one abstract method: getCommonRules. This should return an array of rules that are used on every validation call and should be the bare minimum of rules. In addition you can implement as many extra rule getters as you like - getCreateRules, getUpdateRules, getUpdateAsAdminRules, getFooRules and so on.
Rules are dynamically merged depending on what action you're trying to validate. For example, $validator->validCreate($attributes) will merge getCommonRules and getCreateRules. If getCreateRules doesn't exist, it'll just use getCommonRules. $validator->validUpdateAsAdmin($attributes) will merge getCommonRules and getUpdateAsAdminRules. Rules are merged recursively.
You can tell the validator to replace variables in rules with the replace($key, $value) method. For example, if you have a unique rule and want to dynamically replace the table with a model's table, you can do the following:
public function getCommonRules() {
return ['email' => 'unique:<table>'];
}
$validator->replace('table', $model->getTable());
The class will also replace variables in square brackets with the matching key from input. For example, [foo] will be replaced with whatever the value of 'foo' from the provided input (attributes) are. This way, you can add the value of another input field to a rule (for example, 'end_date' => ['date', 'after:[start_date]'].
Replacing variables will not work in regex rules, as that would potentially break regex operators.
There are a couple of hooks you can use to add custom behaviour. prepareRules($rules, $attributes) will be called after rules are merged and allows you to change validation rules based on input if necessary. This method needs to return the modified array of rules!
prepareValidator($validator) is called before checking if validation passes or not, and can be used to add sometimes() rules or any other custom behaviour onto the Illuminate\Validation\Validator object itself.
Exceptions
You can call $validatorService->toggleExceptions(); to make the validator throw exceptions instead of just returning false. The exception thrown will be of the type anlutro\LaravelValidation\ValidationException, which has some useful methods.
getErrors()gets a flat array of the validation errors.getRules()gets the array of rules that were used when validating.getAttributes()gets the array of input that was validated.
It can also be cast to a string via (string) $exception which will render each validation error on one line.
Contact
Open an issue on GitHub if you have any problems or suggestions.
License
The contents of this repository is released under the MIT license.
anlutro/l4-validation 适用场景与选型建议
anlutro/l4-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.28k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2014 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 anlutro/l4-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anlutro/l4-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-01-13