PHP code example of vjik / yii-validator-scenarios
1. Go to this page and download the library: Download vjik/yii-validator-scenarios library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vjik / yii-validator-scenarios example snippets
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,
) {
}
}
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)],
),
];
}
}
use Yiisoft\Validator\ValidationContext;
use Yiisoft\Validator\Validator;
$result = (new Validator())->validate(
$userDto,
context: new ValidationContext([
On::SCENARIO_PARAMETER => $scenario,
]),
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.