PHP code example of bartosz-maciaszek / validation

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

    

bartosz-maciaszek / validation example snippets




use Validation\Validation as V;

V::validate('foobar', V::string(), function($err, $output) {
    if ($err) {
        echo 'Validation failed: ' . $err;
        exit;
    }
    
    echo $output; // 'foobar'
});

V::validate('[email protected]', V::string()->email(), function($err, $output) {
    // ...
});

V::validate('FooBar', V::string()->lowercase(), function($err, $output) {
    // $output equals 'foobar'!
});

$input = [
    'username' => 'foobar',
    'password' => 'secret123',
    'birthyear' => 1980,
    'email' => '[email protected]',
    'sex' => 'male'
];

$schema = V::arr()->keys([
    'username' => V::string()->alphanum()->min(3)->max(30),
    'password' => V::string()->regex('/[a-z-A-Z0-9]{3,30}/'),
    'birthyear' => V::number()->integer()->min(1900)->max(2013),
    'email' => V::string()->email(),
    'sex' => V::string()->valid('male', 'female')
]);

V::validate($input, $schema, function ($err, $output) {
    // $err === null -> valid!
});