sedlatschek/laravel-conditional-equals-validation
Composer 安装命令:
composer require sedlatschek/laravel-conditional-equals-validation
包简介
Additional rules for conditional equals validation
README 文档
README
This package provides additional validation rules for Laravel projects:
Equals
$request->validate([ 'a' => ['boolean', (new Equals(true))->if('b', false)], 'b' => ['boolean'] ]);
NotEquals
$request->validate([ 'a' => ['string', (new NotEquals('foo'))->ifAnyOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
See Usage for all possibilites! Also know that the native method Rule::when may be a better alternative to this package.
Installation
You can install the package via composer:
composer require sedlatschek/laravel-conditional-equals-validation
Usage
If
Evaluate one other fields value.
$request->validate([ 'a' => ['string', (new Equals('foo'))->if('b', 'bar')], 'b' => ['string'], ]);
IfNot
Evaluate one other fields value.
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifNot('b', 'bar')], 'b' => ['string'], ]);
IfAllOf
Evaluate if all of the given fields match the given value.
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifAllOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
Example validation results:
// passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'bar', ]; // fails $data = [ 'a' => 'x', 'b' => 'bar', 'c' => 'bar', ]; // passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'x', ];
IfAnyOf
Evaluate if any of the given fields match the given value.
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifAnyOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
Example validation results:
// passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'x', ]; // fails $data = [ 'a' => 'x', 'b' => 'bar', 'c' => 'x', ]; // passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'x', ];
IfNoneOf
Evaluate if none of the given fields match the given value.
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifNoneOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
Example validation results:
// passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'x', ]; // fails $data = [ 'a' => 'x', 'b' => 'x', 'c' => 'x', ]; // passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'bar', ];
Combined
All of the above conditions can be chained. The connection between each condition is seen as an and operator.
$request->validate([ 'a' => ['string', (new Equals('foo'))->if('b', 'bar')->ifAnyOf(['c', 'd'], false)->ifAllOf(['e', 'f', 'g'], 1), 'b' => ['string'], 'c' => ['boolean'], 'd' => ['boolean'], 'e' => ['integer'], 'f' => ['integer'], 'g' => ['integer'], ]);
Example validation results:
// passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => true, 'd' => false, 'e' => 1, 'f' => 1, 'g' => 1, ]; // fails $data = [ 'a' => 'x', 'b' => 'bar', 'c' => true, 'd' => false, 'e' => 1, 'f' => 1, 'g' => 1, ]; // passes $data = [ 'a' => 'x', 'b' => 'bar', 'c' => true, 'd' => false, 'e' => 1, 'f' => 1, 'g' => 2, ]; // passes $data = [ 'a' => 'x', 'b' => 'x', 'c' => true, 'd' => false, 'e' => 1, 'f' => 1, 'g' => 1, ]; // passes $data = [ 'a' => 'x', 'b' => 'x', 'c' => true, 'd' => true, 'e' => 1, 'f' => 1, 'g' => 1, ];
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
sedlatschek/laravel-conditional-equals-validation 适用场景与选型建议
sedlatschek/laravel-conditional-equals-validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「equals」 「equal」 「if」 「sedlatschek」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sedlatschek/laravel-conditional-equals-validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sedlatschek/laravel-conditional-equals-validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sedlatschek/laravel-conditional-equals-validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This plugin makes sure that the slug is always the same as the title.
Equatable objects and collections in PHP
EqualsBuilder for PHP.
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.
统计信息
- 总下载量: 3.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-24