PHP code example of estasi / validator

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

    

estasi / validator example snippets




declare(strict_types=1);

use Estasi\Validator\Email;

$email = '[email protected]';
$validator = new Email(Email::ALLOW_UNICODE);
if ($validator->isValid($email)) {
    // your code is here
} else {
    // print "Email "$email" is not correct!"
    echo $validator->getLastErrorMessage();
}



declare(strict_types=1);

use Estasi\Validator\Email;

$email = '[email protected]';
$validator = new Email(Email::ALLOW_UNICODE, [Email::E_INVALID_EMAIL => 'Custom error message.']);
if ($validator->isValid($email)) {
    // your code is here
} else {
    // print "Custom error message."
    echo $validator->getLastErrorMessage();
}



declare(strict_types=1);

use Estasi\Validator\Email;

$email = '[email protected]';
$validator = new Email(Email::ALLOW_UNICODE);
if ($validator->isValid($email)) {
    // your code is here
} else {
    if ($validator->isLastError(Email::E_INVALID_EMAIL)) {
        echo "Custom error message.";
        // or your other code depending on the error
    }
    //...
}



declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'string';
$value = 'string';

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value)) {
    // your code is here
}



declare(strict_types=1);

use Estasi\Validator\Identical;

$context = 'string';
$value = 'string';

$validator = new Identical(null, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}



declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'email';
$value = '[email protected]';
$context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => '[email protected]'];

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}



declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'names.lastname';
$value = 'Doe';
$context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => '[email protected]'];

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}



declare(strict_types=1);

use Estasi\Validator\{Chain,Identical,Regex};

$datum = [
    'password' => [
        'original' => 'password_25',
        'confirm'  => 'password_25'
    ]
];

$chain = new Chain();
$chain = $chain->attach(
                   new Regex('[A-Za-z0-9_]{8,12}', Regex::OFFSET_ZERO, [Regex::OPT_ERROR_VALUE_OBSCURED => true]),
                   Chain::WITH_BREAK_ON_FAILURE
               )
               ->attach(
                   [
                       Chain::VALIDATOR_NAME => 'identical',
                       Chain::VALIDATOR_OPTIONS => [
                           Identical::OPT_TOKEN => 'password.original',
                           Identical::OPT_ERROR_VALUE_OBSCURED => true
                       ]
                   ],
                   Chain::WITH_BREAK_ON_FAILURE
               );
if($chain->isValid($datum['password']['original'], $datum)) {
    // your code is here
}