Skip to content

lefuturiste/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validator

Continuous integration

Simple php validator helper for PSR7 request.

How to use ?

From PSR-7

$validator = new Validator($request->getParsedBody());

From php input

$validator = new Validator($_POST);

Validate methods (or rules)

$validator->required('example');
$validator->notEmpty('example');

And more validate methods (or rules)...

Known if the input is valid

$validator->isValid(); // TRUE|FALSE

Get errors

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);

I18n

English, french and spanish are supported

ValidationLanguage::setLang('fr'); // or `en` or `es`

Tests

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 or composer run tests
  • On linux/mac: vendor/bin/phpunit tests
  • On windows: vendor/bin/phpunit.bat tests