PHP code example of yiisoft / validator

1. Go to this page and download the library: Download yiisoft/validator 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/ */

    

yiisoft / validator example snippets


use Yiisoft\Validator\Rule\AtLeast;
use Yiisoft\Validator\Rule\Email;
use Yiisoft\Validator\Rule\Length;
use Yiisoft\Validator\Rule\Number;
use Yiisoft\Validator\Rule\Required;
use Yiisoft\Validator\Validator;

#[AtLeast(['email', 'phone'])]
final class Person
{
    public function __construct(
        #[Required]
        #[Length(min: 2)]
        public ?string $name = null,

        #[Number(min: 21)]
        public ?int $age = null,

        #[Email]
        public ?string $email = null,

        public ?string $phone = null,
    ) {
    }
}

$person = new Person(
    name: 'John', 
    age: 17, 
    email: '[email protected]',
    phone: null
);

$result = (new Validator())->validate($person);

$result->isValid();

$result->getErrorMessages();