Validate arrays.
$validator = new Validator([
'name' => ['required', 'string'],
'hobbies' => ['array'],
'hobbies.*' => ['string']
]);
$validator->validate([
'name' => 'John',
'hobbies' => ['programming', 'comics', 'workout']
]);
var_dump($validator->failed()); // false
In your project root:
composer require khalyomede\validator:0.*
- Example 1: validating a string
- Example 2: validating a required element
- Example 3: add a new rule
- Example 4: check if a rule already exist or not
- Example 5: using dot syntax to validate multiple elements
require __DIR__ . '/../vendor/autoload.php';
use Khalyomede\Validator;
use Khalyomede\Exception\RuleNotFoundException;
$validator = new Validator([
'name' => ['string']
]);
try {
$validator->validate(['name' => 'John']);
var_dump($validator->failed()); // false
}
catch( RuleNotFoundException $exception ) {
echo "rule {$exception->getRule()} does not exists";
exit(1);
}
require __DIR__ . '/../vendor/autoload.php';
use Khalyomede\Validator;
use Khalyomede\Exception\RuleNotFoundException;
$validator = new Validator(['name' => ['required', 'string']]);
try {
$validator->validate(['name' => 'John']);
var_dump($validator->failed()); // false
}
catch( RuleNotFoundException $exception ) {
echo "rule {$exception->getRule()} does not exist";
exit(1);
}
equire __DIR__ . '/../vendor/autoload.php';
use Khalyomede\Validator;
use Khalyomede\Exception\RuleAlreadyExistException;
use Khalyomede\Exception\RuleNotFoundException;
try {
Validator::extends('longitude', function($value, $key, $items) {
return is_float($value) && ($value >= -180) && ($value <= 180);
});
}
catch( RuleAlreadyExistException $exception ) {
echo "rule {$exception->getRule()} already exist";
exit(1);
}
require __DIR__ . '/../vendor/autoload.php';
use Khalyomede\Validator;
if (Validator::has('ip') === false) {
echo "rule ip does not exist yet";
}
require __DIR__ . '/../vendor/autoload.php';
use Khalyomede\Validator;
$validator = new Validator([
'sith' => ['required', 'array'],
'sith.*' => ['string']
]);
$validator->validate([
'sith' => ['Darth Maul', 'Darth Vador', 'Darth Sidious']
]);
var_dump( $validator->failed() ) // "false", hm... should have been true after all these guys did but anyway
Validate that a key is an array.
$validator = new Validator([
'hobbies' => ['array']
]);
Validate that a key is filled with a valid date in format yyyy-mm-dd
(ISO 8601).
$validator = new Validator([
'created_at' => ['date']
]);
Validate that a key is filled with a valid date in formt yyyy-mm-dd hh:mm:ss
(ISO 8601).
$validator = new Validator([
'updated_at' => ['datetime']
]);
Validate that a key is filled with an email.
$validator = new Validator([
'contact' => ['email']
]);
Validate that a key is filled with a non empty value.
$validator = new Validator([
'name' => ['filled']
]);
Validate that a key is filled with an integer.
$validator = new Validator([
'age' => ['integer']
]);
Validate that a key is filled only with lowercases (non-alpha characters are allowed as well).
$validator = new Validator([
'street' => ['lower']
]);
Validate that a key exists.
$validator = new Validator([
'lastname' => ['present']
]);
Validate that a key is present. The key can be empty by the way.
$validator = new Validator([
'name' => ['required']
]);
Validate that two keys are the same.
$validator = new Validator([
'password' => ['string', 'same:confirmation'],
'confirmation' => ['string']
]);
Validate that a string is a slug (only lowercases, dashes -
allowed).
$validator = new Validator([
'title' => ['slug']
]);
Validate that a key is a string.
$validator = new Validator([
'name' = ['string']
]);
Validate that a key is filled with a time with the format hh:mm:ss
.
$validator = new Validator([
'duration' => ['time']
]);
Validate that a string is only in uppercase.
$validator = new Validator([
'name' => ['upper']
]);
Add a new rule.
Validator::extends('jedi', function($value, $key, $items) {
return in_array($value, ['qui-gon jinn', 'obiwan', 'luke']);
});
Check if the rule already exist.
Validator::has('sith'); // bool(false)