PHP code example of dabrahim / array-validator

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

    

dabrahim / array-validator example snippets


$constraints = array(
    'email' => (object)[
        'prettyName' => 'E-mail',
        'type' => ArrayValidator::TYPE_EMAIL,
    ],
    'firstName' => (object) [
        'prettyName' => 'Prénom',
        'type' => ArrayValidator::TYPE_NAME
    ],
    'lastName' => (object) [
        'prettyName' => 'Nom',
        'type' => ArrayValidator::TYPE_NAME
    ]
);

$a = array(
    'email' => '[email protected]',
    'lastName' => 'Doe',
    'firstName' => 'John'
);

try {
    $av = new ArrayValidator($a, $constraints);
    $av->validate();

    echo "Tout est OK !";

} catch (InvalidValueFormat $e) {
    echo "User error: " .$e->getMessage();

} catch (MissingKeyException $e) {
    echo "User error: " . $e->getMessage();

} catch (Exception $e) {
    echo "Developer error: " . $e->getMessage();
}