bakome/laravel-validation-attributes
Composer 安装命令:
composer require bakome/laravel-validation-attributes
包简介
README 文档
README
This package provides annotations to easily add validation rules on action methods in a controller. Here's a quick example:
use Bakome\ValidationAttributes\Attributes\ValidationRule; class ExampleController { #[ValidationRule('title', 'required|string')] public function actionMethod() { } }
Motivation
This project exists to provide simple and cleaner way to validate request data on Laravel controllers. Using validation rules in action methods often complicate readability of the controller and adding validation in separate requests classes imply switching trough that classes multiple time to view and change the rules (Losing focus). With this simple attributes we can make validation more accessible and maintainable in the controllers without messing the controller code.
Installation
You can install the package via composer:
composer require bakome/laravel-validation-attributes
You can publish the config file with:
php artisan vendor:publish --provider="Bakome\ValidationAttributes\ValidationAttributesServiceProvider" --tag="config"
Usage
The package provides several annotations that should be put on controller action methods. These annotations are used to validate request data.
Adding a validation rule with string as a parameter
use Bakome\ValidationAttributes\Attributes\ValidationRule; class ExampleController { #[ValidationRule('title', 'required|string')] public function actionMethod() { } }
This attribute will check if title parameter is present in current request and throw validation error if not present.
Adding a validation rule with an array as a parameter
use Bakome\ValidationAttributes\Attributes\ValidationRule; class ExampleController { #[ValidationRule('title', ['required', 'string'])] public function actionMethod() { } }
This attribute will check if title parameter is present in current request and throw validation error if not present.
Adding a multiple validation rules
use Bakome\ValidationAttributes\Attributes\ValidationRule; class ExampleController { #[ValidationRule('description', 'required|string')] #[ValidationRule('title', ['required', 'string'])] public function actionMethod() { } }
This attribute will check if title and description parameters are present in current request and throw validation error if not present.
Adding a redirect for failed validation
use Bakome\ValidationAttributes\Attributes\ValidationRule; use Bakome\ValidationAttributes\Attributes\ValidationRuleRedirect; class ExampleController { #[ValidationRule('description', 'required|string')] #[ValidationRule('title', ['required', 'string'])] #[ValidationRuleRedirect('/a-redirection-route')] public function actionMethod() { } }
This attribute will check if title and description parameters are present in current request, compile validation error messages and redirect the page to provided route.
Adding a redirect with input for failed validation
use Bakome\ValidationAttributes\Attributes\ValidationRule; use Bakome\ValidationAttributes\Attributes\ValidationRuleRedirect; class ExampleController { #[ValidationRule('description', 'required|string')] #[ValidationRule('title', ['required', 'string'])] #[ValidationRuleRedirect('/a-redirection-route', true)] public function actionMethod() { } }
This attribute will check if title and description parameters are present in current request, compile validation error messages and redirect the page to provided route including old input parameters.
Ajax's requests awareness and proper json responses returned for failed validation
Config
Plugin enabled/disabled
'enabled' => true,
This plugin can be enabled or disabled with altering this property in configuration file. Default value is true (Enabled).
Plugin enabled/disabled
'middleware' => \Bakome\ValidationAttributes\Routing\Middleware\ValidationRulesByAttributes::class,
This property enables developers to provide a different handler for Validation via Middleware. Please be careful with this option and don't change it if you don't need custom behaviour.
Validation middleware
'middleware' => \Bakome\ValidationAttributes\Routing\Middleware\ValidationRulesByAttributes::class,
This property enables developers to provide a different handler for Validation via Middleware. Please be careful with this option and don't change it if you don't need custom behaviour.
Detect api routes
'api_pattern' => 'api/*',
This property provide value for detecting api routes. Using expects json sometimes is not enough and when that lacks this option/feature do the job.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
Roadmap
- Provide custom validation messages
- Provide validation groups to reduce boilerplate code
bakome/laravel-validation-attributes 适用场景与选型建议
bakome/laravel-validation-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「attributes」 「laravel-validation-attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bakome/laravel-validation-attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bakome/laravel-validation-attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bakome/laravel-validation-attributes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
The last validation library you will ever need!
A PHPDoc docblock interpreter that is simple, easy to use, and provides Attribute alternatives.
Module with SP files for attribute_check
PSR-11 Container for Dependency Injection (auto-wiring, attributes, factories, closures, array access)
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-25