PHPackages                             vjik/yii-validator-scenarios - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. vjik/yii-validator-scenarios

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

vjik/yii-validator-scenarios
============================

The scenario feature for Yii Validator

2.0.0(1y ago)23BSD-3-ClausePHPPHP ^8.1

Since Feb 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/vjik/yii-validator-scenarios)[ Packagist](https://packagist.org/packages/vjik/yii-validator-scenarios)[ RSS](/packages/vjik-yii-validator-scenarios/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

 [![](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667)](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667)

Yii Validator Scenarios
=======================

[](#yii-validator-scenarios)

[![Latest Stable Version](https://camo.githubusercontent.com/27021a5e660babdb552e451c2c67c99d292104326c9626f40cfd807b91e65ec5/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f7969692d76616c696461746f722d7363656e6172696f732f76)](https://packagist.org/packages/vjik/yii-validator-scenarios)[![Total Downloads](https://camo.githubusercontent.com/a3cf7ab84a0ac247193c52397b95dbc1bfaf68bdf8ae3e33b14eb4b5f75f9037/68747470733a2f2f706f7365722e707567782e6f72672f766a696b2f7969692d76616c696461746f722d7363656e6172696f732f646f776e6c6f616473)](https://packagist.org/packages/vjik/yii-validator-scenarios)[![Build status](https://github.com/vjik/yii-validator-scenarios/actions/workflows/build.yml/badge.svg)](https://github.com/vjik/yii-validator-scenarios/actions/workflows/build.yml)[![Coverage Status](https://camo.githubusercontent.com/2dd1916fb69be3c2cb6d20e2477c59052e748be216818caef7241c93689c9241/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f766a696b2f7969692d76616c696461746f722d7363656e6172696f732f62616467652e737667)](https://coveralls.io/github/vjik/yii-validator-scenarios)[![Mutation testing badge](https://camo.githubusercontent.com/1f4cb24057f3955d229b33998ab0e67b377af0b3ec256dcd0e468ab74792b337/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246766a696b2532467969692d76616c696461746f722d7363656e6172696f732532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/vjik/yii-validator-scenarios/master)[![type-coverage](https://camo.githubusercontent.com/f8f12f9d096e11098c927684d26157817e1563da510bb95f5103792caafd177e/68747470733a2f2f73686570686572642e6465762f6769746875622f766a696b2f7969692d76616c696461746f722d7363656e6172696f732f636f7665726167652e737667)](https://shepherd.dev/github/vjik/yii-validator-scenarios)[![static analysis](https://github.com/vjik/yii-validator-scenarios/workflows/static%20analysis/badge.svg)](https://github.com/vjik/yii-validator-scenarios/actions?query=workflow%3A%22static+analysis%22)[![psalm-level](https://camo.githubusercontent.com/1477f1908abf72fecc093f0b1c0e2d58b7e1058f9738a9f9dd638f12fd4f50cf/68747470733a2f2f73686570686572642e6465762f6769746875622f766a696b2f7969692d76616c696461746f722d7363656e6172696f732f6c6576656c2e737667)](https://shepherd.dev/github/vjik/yii-validator-scenarios)

The package provides validator rule `On` that implement the scenario feature for [Yii Validator](https://github.com/yiisoft/validator).

Requirements
------------

[](#requirements)

- PHP 8.1 or higher.

Installation
------------

[](#installation)

The package could be installed with [composer](https://getcomposer.org/download/):

```
composer require vjik/yii-validator-scenarios
```

General usage
-------------

[](#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**

AttrubuteRules`name``Required`, `Length``email``Required`, `Email``password``Required`, `Length`**login**

AttrubuteRules`name`—`email``Required`, `Email``password``Required`, `Length`**Without scenario**

AttrubuteRules`name`—`email``Required`, `Email``password`—`On` rule parameters
--------------------

[](#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
-------

[](#testing)

### Unit testing

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```
./vendor/bin/phpunit
```

### Mutation testing

[](#mutation-testing)

The package tests are checked with [Infection](https://infection.github.io/) mutation framework with [Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:

```
./vendor/bin/roave-infection-static-analysis-plugin
```

### Static analysis

[](#static-analysis)

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:

```
./vendor/bin/psalm
```

License
-------

[](#license)

The Yii Validator Scenarios is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~624 days

Total

2

Last Release

551d ago

Major Versions

1.0.0 → 2.0.02024-11-08

PHP version history (2 changes)1.0.0PHP ^8.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/53e5ee1dedd50f71e4aeeac2929f786cdfb400359d4776e6cd806388d0d5df2c?d=identicon)[vjik](/maintainers/vjik)

---

Top Contributors

[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (15 commits)")

---

Tags

scenariosvalidationvalidatoryii3validatorvalidationyiigroupsscenarios

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/vjik-yii-validator-scenarios/health.svg)

```
[![Health](https://phpackages.com/badges/vjik-yii-validator-scenarios/health.svg)](https://phpackages.com/packages/vjik-yii-validator-scenarios)
```

###  Alternatives

[vlucas/valitron

Simple, elegant, stand-alone validation library with NO dependencies

1.6k4.4M128](/packages/vlucas-valitron)[wixel/gump

A fast, extensible &amp; stand-alone PHP input validation class that allows you to validate any data.

1.2k1.3M30](/packages/wixel-gump)[particle/validator

Flexible and highly usable validation library with no dependencies.

2521.7M10](/packages/particle-validator)[phpexperts/datatype-validator

An easy to use data type validator (both strict and fuzzy).

141.1M2](/packages/phpexperts-datatype-validator)[codeonyii/yii2-at-least-validator

Validates at least one (or more) attributes.

28253.5k1](/packages/codeonyii-yii2-at-least-validator)[dragon-code/card-number

Generation and verification of card numbers using Luhn's algorithm.

6512.8k](/packages/dragon-code-card-number)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
