PHP code example of initphp / validation

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

    

initphp / validation example snippets




use InitPHP\Validation\Validation;

// GET /?name=Muhammet&year=2022
$validation = new Validation($_GET);

$validation->rule('name', 'reach ($validation->getError() as $message) {
        echo $message . "\n";
    }
}

$validation = new Validation([
    'email'    => '[email protected]',
    'password' => 'secret',
    'confirm'  => 'secret',
]);

$valid = $validation
    ->rule('email', '

$validation = new Validation(['number' => 13]);

$validation->rule('number', static function ($value): bool {
    return ($value % 2) === 0;
}, '{field} must be an even number.');

$validation->validation();            // false
$validation->getError();              // ["number must be an even number."]

$validation->rule('number', ['integer', static fn ($v): bool => $v > 0]);

$validation->extend(
    'divisible',
    static fn ($value, $by): bool => ((int) $value % (int) $by) === 0,
    '{field} must be divisible by {2}.'
);

$validation->rule('quantity', 'divisible(5)');

$validation->rule('nickname', 'optional|alpha|length(3...20)');

$validation->pattern('product_code', '[A-Z]{2}-[0-9]{4}');
$validation->rule('code', 'regex(product_code)');

$validation->rule('age', 'min(18)');
// "age must be greater than or equal to 18."

$validation->labels(['age' => 'Age']);
// "Age must be greater than or equal to 18."

$validation->setLocale('tr');
$validation->setLocaleArray(['integer' => '{field} is not a whole number.']);