TL;DR An validation tool for arrays filled by foreign input like forms, json data, query parameters etc. The name is from Old Norse language and means defender.
The usual way...
composer require tflori/verja
Initialize a container, set the input data, define filters and validators, validate the data, get the data.
$gate = new Verja\Gate();
$gate->accepts([
'username' => ['notEmpty', 'strLen:3:20'],
'password' => ['notEmpty', 'strLen:8'],
'email' => ['notEmpty', 'email'],
]);
if ($gate->validate($_POST)) {
// how ever your orm works..
$user = new User($gate->getData());
$user->save();
} else {
$errors = $gate->getErrors();
}
If you prefer auto completion you can of course pass objects:
use Verja\Validator;
$gate->accepts([
'username' => (new Field())
->addValidator(new Validator\NotEmpty())
->addValidator(new Validator\StrLen(3, 20)),
'password' => [new Validator\NotEmpty(), new Validator\StrLen(8)],
'email' => ['notEmpty', new App\Validator\DomainEmail('my-domain.com')]
]);
For more information check the documentation on github.io/verja.
In this library the following validators are included:
After
: Value must be a date time after$dateTime
Alpha
: Value must contain only alphabetical charactersAlphaNumeric
: Value must contain only alphabetic and numeric charactersBefore
: Value must be a date time before$dateTime
Boolean
: Value must be booleanBetween
: Value must be between$min
and$max
Contains
: Value must contain$subString
CreditCard
: Value must be a valid credit card numberDateTime
: Value must be a valid date in$format
EmailAddress
: Value must be a valid email addressEquals
: Field must match field$opposide
InArray
: Value must exist in$array
Integer
: Value must be integerIpAddress
: Value must be a valid IP address of$version
IsArray
: Value must be an arrayNotEmpty
: Value must not be emptyNumeric
: Value must be numericPregMatch
: Value must match regular expression$pattern
Slug
: Value must contain only slug characters (a-z, 0-9, -, _)StrLen
: String length from value must be between$min
and$max
Truthful
: Converted to boolean the value must be trueUrl
: Value must be a valid URL
The following filters are included in this library:
Boolean
: Converts integer and string values to booleanConvertCase
: Converts case to$mode
(upper, lower or title)DateTime
: Converts string from$format
inDateTime
objectEscape
: Escape special characters for usage in htmlInteger
: Converts string values to integerNumeric
: Converts string values to float or integerPregReplace
: Replaces$pattern
with$replace
(replace can also be a callback)Replace
: Replaces$search
in values with$replace
Trim
: Trims$charcterMask
from values