awurth / slim-validation
A wrapper around the respect/validation PHP validation library for easier error handling and display
Installs: 297 825
Dependents: 11
Suggesters: 0
Security: 0
Stars: 68
Watchers: 5
Forks: 23
Open Issues: 2
Requires
- php: >=8.1
- respect/validation: ^2.0
- symfony/options-resolver: ^6.0 || ^7.0
- symfony/property-access: ^6.0 || ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.11
- phpunit/phpunit: ^9.5
- psr/http-message: ^1.0
- slim/psr7: ^1.5
- twig/twig: ^3.5
README
A wrapper around the Respect Validation PHP validation library for easier error handling and display
This project was originally designed to be used with the Micro-Framework "Slim", hence the name "Slim Validation", but can now be used in any other PHP project.
Installation
$ composer require awurth/slim-validation
Documentation
Usage
The following example shows how to validate that a string is at least 10 characters long:
use Awurth\Validator\Validator; use Respect\Validation\Validator as V; $validator = Validator::create(); $failures = $validator->validate('Too short', V::notBlank()->length(min: 10)); if (0 !== $failures->count()) { // Validation failed: display errors foreach ($failures as $failure) { echo $failure->getMessage(); } }
The validate()
method returns a list of validation failures as an object that implements ValidationFailureCollectionInterface
. If you have lots of validation failures, you can filter them with a callback:
use Awurth\Validator\ValidationFailureInterface; $failures = $validator->validate(/* ... */); $filteredFailures = $failures->filter(static function (ValidationFailureInterface $failure, int $index): bool { return $failure->getRuleName() === 'notBlank'; });
License
This package is available under the MIT license.