PHP code example of rasuvaeff / yii3-respect-validation
1. Go to this page and download the library: Download rasuvaeff/yii3-respect-validation 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/ */
rasuvaeff / yii3-respect-validation example snippets
use Rasuvaeff\Yii3RespectValidation\RespectRule;
use Respect\Validation\Validators\AllOf;
use Respect\Validation\Validators\Between;
use Respect\Validation\Validators\Length;
use Respect\Validation\Validators\StringType;
final class RegisterForm
{
#[RespectRule(new AllOf(new StringType(), new Length(new Between(1, 50))))]
public string $username = '';
}
use Yiisoft\Validator\Validator;
$result = (new Validator())->validate(new RegisterForm());
$result->isValid(); // false
$result->getPropertyErrorMessages('username'); // ['Username must be between 1 and 50']
#[RespectRule(new StringType())]
public string $username = '';
// equivalent, built at runtime instead of in the attribute:
$rule = new RespectRule(v::stringType());
use Respect\Validation\Validator as v;
use Yiisoft\Validator\RulesProviderInterface;
final class RegisterForm implements RulesProviderInterface
{
public string $username = '';
public string $email = '';
public function getRules(): iterable
{
return [
'username' => new RespectRule(v::stringType()->length(v::between(1, 50))),
'email' => new RespectRule(v::email()),
];
}
}