PHP code example of redbox / validator
1. Go to this page and download the library: Download redbox/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/ */
redbox / validator example snippets
use Redbox\Validation\Validator;
$validator = new Validator([
'foo' => 'The quick brown fox jumps over the lazy dog'
]);
$data = $validator->validate([
'food' => 'string'
]);
/**
* Validator::passes() is also
* available.
*/
if ($validator->fails()) {
/**
* Handle errors use $validator->errors()
* to get the errors.
*/
}
use Redbox\Validation\ValidationContext;
use Redbox\Validation\Validator;
$validator = new Validator([
'foo' => 'The quick brown fox jumps over the lazy dog'
]);
$data = $validator->validate([
/**
* This will validate field 'foo' equals 'baz' in this case it will not so
* the validation will fail and an error will be set for field 'foo'.
*/
'foo' => function (ValidationContext $context): bool {
return ($context->keyExists() && $context->getValue() === 'baz')
or $context->addError("Field {$context->getKey()} does not equal 'baz'.");
}
]);
/**
* Validator::passes() is also
* available.
*/
if ($validator->fails()) {
/**
* Handle errors use $validator->errors()
* to get the errors.
*/
print_r($validator->errors());
}