Slick Validator package
, (*1)
Slick/Validator
is a set of input validation tools that can be used to check your input data.
It also has the concept of Validation Chain that can combine validators for a specific validation., (*2)
This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It
also applies the semantic version 2.0.0 specification., (*3)
Install
Via Composer, (*4)
``` bash
$ composer require slick/validator, (*5)
## Usage
One of the easiest ways of using a validator is using `StaticValidator` class to check a value:
```php
use Slick\Validator\StaticValidator;
if (StaticValidator::validates('notEmpty', $value) {
// Some code with valid value
} else {
print StaticValidator::getMessage(); // Print out validation messages
}
Slick/Validator
comes with the following validators:, (*6)
Alias |
Description |
Class |
notEmpty |
Fails if passed value is an empty string |
Slick\Validator\NotEmpty |
email |
Fails if passed value is not a valid e-mail address |
Slick\Validator\Email |
number |
Fails if passed value is not am integer number |
Slick\Validator\Number |
alphaNumeric |
Fails if passed value is not an alpha numeric value |
Slick\Validator\AlphaNumeric |
url |
Fails if passed value is not an URL |
Slick\Validator\URL |
StaticValidator
is also a validator objects factory. For example:, (*7)
use Slick\Validator\StaticValidator;
$urlValidator = StaticValidator::create('notEmpty', 'You must enter a valid URL.');
if ($urlValidator->validates($_POST['url']) {
// URL is valid use it...
} else {
print $urlValidator->getMessage(); // Will print out 'You must enter a valid URL.'
}
Combining various validator to use it as a single validation can be done with
ValidationChain
., (*8)
use Slick\Validator\StaticValidator;
use Slick\Validator\ValidationChain;
$emailValidation = new ValidationChain();
$emailValidation
->add(StaticValidator::create('notEmpty', 'Email address cannot be empty.'))
->add(StaticValidator::create('email', 'The entered e-mail is not a valid address.');
if ($emailValidation->validates($_POST['email']) {
// URL is valid use it...
} else {
print implode(', ', $emailValidation->getMessages());
// Will print out the validation messages for the validator(s) that fail.
}
You can always create your own validator and use the StaticValidator
or the ValidationChain
as long
as you implement the Slick\Validator\ValidatorInterface
or Slick\Validator\ValidationChainInterface
., (*9)
Testing
bash
$ vendor/bin/phpunit
, (*10)
Contributing
Please see CONTRIBUTING for details., (*11)
Security
If you discover any security related issues, please email silvam.filipe@gmail.com instead of using the issue tracker., (*12)
Credits
License
The MIT License (MIT). Please see License File for more information., (*13)