slick/validator

Slick/Validator is a simple library with useful input validators

v1.1.0 2016-02-02 17:25 UTC

This package is auto-updated.

Last update: 2024-03-20 10:11:05 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

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.

This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification.

Install

Via Composer

$ composer require slick/validator

Usage

One of the easiest ways of using a validator is using StaticValidator class to check a value:

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:

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:

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.

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.

Testing

$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email silvam.filipe@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.