03/03
2018
Wallogit.com
2017 © Pedro Peláez
PHP Validator class.
Validator is a framework agnostic and fluent server side form validation package for PHP, (*1)
Validator can be installed via composer just execute the following command
in your project root, (*2)
composer require badelal2007/validator, (*3)
Or add the following in your composer.json file and run composer install, (*4)
"require": {
"badelal2007/validator": "dev-master"
}
Using validator in your project is super simple, here is an example, (*5)
require_once __DIR__ . '/vendor/autoload.php';
$validator = new validator\Validator();
$validator->validate($_POST, [
'username' => 'required',
'password' => 'required|password:strong',
'confirm_password' => 'required|same:password'
]);
#Return true if validation pass
var_dump($validator->passed());
#Return true if validation failed
var_dump($validator->failed());
#Return all errores if any
var_dump($validator->getErrors());
#Print first error if any for 'password' field
if($errors->get('password')) { echo '<span class="error">'.$errors->get('password')->first().'</span>'; }
#Print last error if any for 'password' field
if($errors->get('password')) { echo '<span class="error">'.$errors->get('password')->last().'</span>'; }
#Change 'message' for validation rule
$validator->validate($_POST, [
'email' => 'required--message=Please enter email|email--Please enter valid email ID.',
'password' => 'required'
]);
If you've files to validate you will need to merge $_POST|$_GET and with $_FILES just like the following, (*6)
$validator = new validator\Validator();
$validator->validate(array_merge($_POST, $_FILES), [
'profile_picture' => 'file:image'
]);
Accepts password strength like password:strong|medium|normal (default noraml) Accepts file type currently supported formats: image,video,doc