Simple php validator helper for PSR7 request.
$validator = new Validator($request->getParsedBody());
$validator = new Validator($_POST);
$validator->required('example');
$validator->notEmpty('example');
And more validate methods (or rules)...
$validator->isValid(); // TRUE|FALSE
To get the list of all the errors that your input have you can use the getErrors() method which return all the errors as an array:
$validator->getErrors();
You can get errors in a different format, with the rules as key:
$validator->getErrors(\Validator\ValidationError::FORMAT_WITH_KEYS);
Or as a array of array, with each array representing an error with the key 'code' and 'message' (format to use in an JSON:API compliant API):
$validator->getErrors(\Validator\ValidationError::FORMAT_ARRAY);
If you dont' want to specify each time the ValidationError format you can use this static call to set as a setting for the whole project. For example if you want to set the FORMAT_ARRAY as the default format for the whole project you can use this piece of code:
\Validator\ValidationError::setDefaultFormat(\Validator\ValidationError::FORMAT_ARRAY);
English, french and spanish are supported
ValidationLanguage::setLang('fr'); // or `en` or `es`
All the tests are in the tests
folder. You can run tests with theses commands (do a composer install before).
- With composer:
composer run test
orcomposer run tests
- On linux/mac:
vendor/bin/phpunit tests
- On windows:
vendor/bin/phpunit.bat tests