inani / phpvalidate
The lightest way to validate your inputs.
1.0
2020-02-25 14:09 UTC
This package is auto-updated.
Last update: 2024-10-29 05:41:12 UTC
README
You have some input data to validate but you don't want to grab the huge libraries to validate them? Well here is the solution, PHPValidate is a very light libraries that allows you to validate simple data and enables you to create custom constraint at any time! Come on give it a try!
Installation
composer require inani/phpvalidate
Usage
Feed the Validator with the array data to verifies
$validator = new Validator([ 'name' => 'Inani El Houssain']);
Build your Rules
// Only if required and without length greater or equal than 10 characters $validator->addRule(( new Rule('name'))->required()->min(10));
Check the rules
$bool = $validator->check();
Get the errors list
// get the errors of the name field, all the fields if not specified $validator->getErrors('name');
Available rules
Custom Constraints
Every custom rule should extends the CustomConstraint Class and implements the two methods, for instance creating a rule that validates an email field:
<?php use Inani\PHPValidate\CustomConstraint; class ShouldBeAnEmail extends CustomConstraint { /** * Check if the field is Required * * @param null $value * @return bool */ public function isValid($value = null) { if(!filter_var($value, FILTER_VALIDATE_EMAIL)){ $this->errorFound(); return false; } return true; } public function getError() { return "This is not a valid email"; } }
And its usage is as simple as injecting the Rule in the rules array like this :
$validator->addRule(( new Rule('email'))->required()->inject(new ShouldBeAnEmail));