PHP code example of z7zmey / rvalidate

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

    

z7zmey / rvalidate example snippets


use RValidate\Validator;
use RValidate\Sub;
use RValidate\Validators as V;
use RValidate\Filters as F;

$data = [
    'login' => 'Nick',
    'email' => '[email protected]',
    'password' => '*****'
];

$pattern = [
    new V\IsArray(),
    new V\keys(['login', 'email', 'password', 'name']),
    new Sub('login', [new V\IsString(), new V\Min(5)]),
    new Sub('email', new V\Email()),
    new Sub('password', new V\Regex('/[A-Za-z[:punct:] ]{6,}/')),
];

try {
    $result = Validator::run($data, $pattern);
} catch (\RValidate\Exceptions\ValidateException $e) {
    foreach ($e->getErrorsIterator() as $error) {
        echo $error . PHP_EOL;
    }
}

$data = [
    'id_user' => 1011,
    'roles' => [
        'admin' => false,
        'moderator' => true,
        'tester' => false
    ]
];

$pattern = [
    new V\IsArray(),
    new V\keys(['id_user', 'roles']),
    new Sub(new F\Key\Equal('id_user'), [new V\IsInteger()]),
    new Sub(new F\Key\Equal('roles'),   [
        new V\Keys(['admin', 'moderator', 'tester']),
        new Sub(new F\Key\Equal('admin'),     [new V\IsBoolean()]),
        new Sub(new F\Key\Equal('moderator'), [new V\IsBoolean()]),
        new Sub(new F\Key\Equal('tester'),    [new V\IsBoolean()]),
    ]),
];

$data = [
    'String1'  => 'some string',
    'String2'  => 'some string',
    'String3'  => '',
    'Number1'  => 1,
    'Number2'  => 2,
    'Number44' => 44,
    'Alnum1'   => 'alpha1'
];

$pattern = [
    new V\IsArray(),
    new Sub(new F\Key\Regex('/^String\d+$/'), [new V\IsString()]),
    new Sub(new F\Key\Regex('/^Number\d+$/'), [new V\IsInteger()]),
    new Sub(new F\Key\Regex('/^Alnum\d+$/'), [new V\Alnum(), new V\NotEmpty()]),
];