PHP code example of graze / data-validator
1. Go to this page and download the library: Download graze/data-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/ */
graze / data-validator example snippets
use Graze\DataValidator\DataValidator;
$validator = new DataValidator();
// Add a processor to roughly capitalize first names.
$validator->addProcessor(function (array $data) {
$data['first_name'] = ucfirst($data['first_name']);
return $data;
});
// Add a validator to check against a 'reserved' list.
$validator->addValidator(function (array $data) {
if (in_array($data['first_name'], ['Sam', 'John', 'Ray'])) {
return 'reserved_name';
}
});
/** @var array */
$processed = $validator->process(['first_name' => 'sam']);
/** @var array */
$failures = $validator->validate($processed);
var_dump($failures);
array(1) {
["reserved_name"]=>
bool(true)
}