vjik/yii-validator-scenarios
Composer 安装命令:
composer require vjik/yii-validator-scenarios
包简介
The scenario feature for Yii Validator
README 文档
README
Yii Validator Scenarios
The package provides validator rule On that implement the scenario feature
for Yii Validator.
Requirements
- PHP 8.1 or higher.
Installation
The package could be installed with composer:
composer require vjik/yii-validator-scenarios
General usage
The scenario feature implement via the rule On and a validation context parameter.
Configure rules:
use Vjik\Yii\ValidatorScenarios\On; use Yiisoft\Validator\Rule\Email; use Yiisoft\Validator\Rule\Length; use Yiisoft\Validator\Rule\Required; final class UserDto { public function __construct( #[On( 'register', [new Required(), new Length(min: 7, max: 10)] )] public string $name, #[Required] #[Email] public string $email, #[On( ['login', 'register'], [new Required(), new Length(min: 8)], )] public string $password, ) { } }
Or same without attributes:
use Vjik\Yii\ValidatorScenarios\On; use Yiisoft\Validator\Rule\Email; use Yiisoft\Validator\Rule\Length; use Yiisoft\Validator\Rule\Required; use Yiisoft\Validator\RulesProviderInterface; final class UserDto implements RulesProviderInterface { public function __construct( public string $name, public string $email, public string $password, ) { } public function getRules(): iterable { return [ 'name' => new On( 'register', [new Required(), new Length(min: 7, max: 10)], ), 'email' => [new Required(), new Email()], 'password' => new On( ['login', 'register'], [new Required(), new Length(min: 8)], ), ]; } }
Pass the scenario to the validator through the context:
use Yiisoft\Validator\ValidationContext; use Yiisoft\Validator\Validator; $result = (new Validator())->validate( $userDto, context: new ValidationContext([ On::SCENARIO_PARAMETER => $scenario, ]), );
Rules that will be applied according to scenarios:
register
| Attrubute | Rules |
|---|---|
name |
Required, Length |
email |
Required, Email |
password |
Required, Length |
login
| Attrubute | Rules |
|---|---|
name |
— |
email |
Required, Email |
password |
Required, Length |
Without scenario
| Attrubute | Rules |
|---|---|
name |
— |
email |
Required, Email |
password |
— |
On rule parameters
$scenario
The scenario(s) that $rules are in. null if rules used always. Defaults to null.
$rules
Rules that will be applied according to $scenario. Defaults to empty array.
$not
Whether the scenario check should be inverted. When this parameter is set true, the validator checks whether
the current scenario is among $scenario and if NOT, $rules will be applied. Defaults to false.
$skipOnEmpty
Whether skip $rules on empty value or not, and which value consider as empty. Defaults to null.
$skipOnError
A boolean value where true means to skip $rules when the previous one errored and false — do not skip.
Defaults to false.
$when
The closure that allow to apply $rules under certain conditions only. Defaults to null.
Testing
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:
./vendor/bin/roave-infection-static-analysis-plugin
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
License
The Yii Validator Scenarios is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
vjik/yii-validator-scenarios 适用场景与选型建议
vjik/yii-validator-scenarios 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validator」 「validation」 「yii」 「groups」 「scenarios」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vjik/yii-validator-scenarios 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vjik/yii-validator-scenarios 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vjik/yii-validator-scenarios 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
Priveate for SkeekS CMS
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-12-12