tflori / verja
An validation tool for arrays filled by foreign input
v1.2.1
2024-04-03 11:20 UTC
Requires
- php: ^7.0 || ^8.0
- ext-json: *
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-04 16:08:11 UTC
README
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.
Installation
The usual way...
composer require tflori/verja
Usage
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.
Predefined Validators
In this library the following validators are included:
After: Value must be a date time after$dateTimeAlpha: Value must contain only alphabetical charactersAlphaNumeric: Value must contain only alphabetic and numeric charactersBefore: Value must be a date time before$dateTimeBoolean: Value must be booleanBetween: Value must be between$minand$maxContains: Value must contain$subStringCreditCard: Value must be a valid credit card numberDateTime: Value must be a valid date in$formatEmailAddress: Value must be a valid email addressEquals: Field must match field$opposideInArray: Value must exist in$arrayInteger: Value must be integerIpAddress: Value must be a valid IP address of$versionIsArray: Value must be an arrayNotEmpty: Value must not be emptyNumeric: Value must be numericPregMatch: Value must match regular expression$patternSlug: Value must contain only slug characters (a-z, 0-9, -, _)StrLen: String length from value must be between$minand$maxTruthful: Converted to boolean the value must be trueUrl: Value must be a valid URL
Predefined Filters
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$formatinDateTimeobjectEscape: Escape special characters for usage in htmlInteger: Converts string values to integerNumeric: Converts string values to float or integerPregReplace: Replaces$patternwith$replace(replace can also be a callback)Replace: Replaces$searchin values with$replaceTrim: Trims$charcterMaskfrom values