https://github.com/awurth/slimvalidation
A wrapper around the Respect Validation PHP validation library for easier error handling and display
https://github.com/awurth/slimvalidation
php respect slim twig-extension validation validator
Last synced: 6 months ago
JSON representation
A wrapper around the Respect Validation PHP validation library for easier error handling and display
- Host: GitHub
- URL: https://github.com/awurth/slimvalidation
- Owner: awurth
- License: mit
- Created: 2016-12-09T20:31:39.000Z (almost 9 years ago)
- Default Branch: 5.x
- Last Pushed: 2023-12-18T12:53:13.000Z (almost 2 years ago)
- Last Synced: 2024-10-24T18:04:58.213Z (12 months ago)
- Topics: php, respect, slim, twig-extension, validation, validator
- Language: PHP
- Homepage:
- Size: 186 KB
- Stars: 68
- Watchers: 5
- Forks: 23
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Slim Validation
[](https://github.com/awurth/SlimValidation/actions/workflows/ci.yml)
[](https://packagist.org/packages/awurth/slim-validation)
[](https://packagist.org/packages/awurth/slim-validation)[](https://packagist.org/packages/awurth/slim-validation)
[](https://packagist.org/packages/awurth/slim-validation)A wrapper around the [Respect Validation](https://github.com/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
``` bash
$ composer require awurth/slim-validation
```## Documentation
* [**5.x**](https://github.com/awurth/SlimValidation/tree/5.x/docs) (current, PHP >= 8.1)
* [**3.4**](https://github.com/awurth/SlimValidation/tree/3.x/docs) (outdated, PHP >= 7.1)## Usage
The following example shows how to validate that a string is at least 10 characters long:
``` php
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`](src/ValidationFailureCollectionInterface.php). If you have lots of validation failures, you can filter them with a callback:
``` php
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](LICENSE).