The package provides validator rule On
that implement the scenario feature
for Yii Validator.
- PHP 8.0 or higher.
The package could be installed with composer:
composer require vjik/yii-validator-scenarios
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 |
— |
$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
.
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:
./vendor/bin/roave-infection-static-analysis-plugin
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
The Yii Validator Scenarios is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.