sedlatschek / laravel-conditional-equals-validation
条件等于验证的附加规则
v1.0.0-beta.7
2023-07-19 06:23 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
This package is auto-updated.
Last update: 2024-09-19 08:38:19 UTC
README
此包为Laravel项目提供附加验证规则
等于
$request->validate([ 'a' => ['boolean', (new Equals(true))->if('b', false)], 'b' => ['boolean'] ]);
不等于
$request->validate([ 'a' => ['string', (new NotEquals('foo'))->ifAnyOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
查看使用方法以了解所有可能性!还请注意,本地方法 Rule::when 可能是此包的更好替代方案。
安装
您可以通过composer安装此包
composer require sedlatschek/laravel-conditional-equals-validation
使用方法
如果
评估其他字段的值。
$request->validate([ 'a' => ['string', (new Equals('foo'))->if('b', 'bar')], 'b' => ['string'], ]);
如果不
评估其他字段的值。
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifNot('b', 'bar')], 'b' => ['string'], ]);
如果所有
评估所有给定字段是否与给定值匹配。
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifAllOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
示例验证结果
// passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'bar', ]; // fails $data = [ 'a' => 'x', 'b' => 'bar', 'c' => 'bar', ]; // passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'x', ];
如果任何一个
评估给定字段中是否有任何一个与给定值匹配。
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifAnyOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
示例验证结果
// passes $data = [ 'a' => 'foo', 'b' => 'bar', 'c' => 'x', ]; // fails $data = [ 'a' => 'x', 'b' => 'bar', 'c' => 'x', ]; // passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'x', ];
如果没有一个
评估给定字段中是否没有一个与给定值匹配。
$request->validate([ 'a' => ['string', (new Equals('foo'))->ifNoneOf(['b', 'c'], 'bar')], 'b' => ['string'], 'c' => ['string'], ]);
示例验证结果
// passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'x', ]; // fails $data = [ 'a' => 'x', 'b' => 'x', 'c' => 'x', ]; // passes $data = [ 'a' => 'foo', 'b' => 'x', 'c' => 'bar', ];
组合
所有上述条件都可以链式调用。每个条件之间的连接被视为一个 and 操作符。
$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'], ]);
示例验证结果
// 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, ];
测试
composer test
变更日志
请参阅变更日志,了解最近有哪些变化。
鸣谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。