PHP Data Validator
Make your apps validation easily (inspired by Laravel Validation), (*1)
, (*2)
Page Index:
- Quick Start
- Contributing
- Testing
- License, (*3)
Suggested Links:
- Installation
- Feature Guide
- Rules
- Existing Rules
- Customization
- Formatting Message
- Integrations, (*4)
, (*5)
Quick start :rocket:
Setting up the validationFacade:, (*6)
<?php
use Respect\Validation\Validator as RespectValidator;
use TheSupportGroup\Common\Validator\Helpers;
use TheSupportGroup\Common\ValidationAdaptor\ValidationAdaptor;
// Prep up the validator, ideally done using DI.
$respectValidator = new RespectValidator();
$errorBag = new Helpers\FieldsErrorBag();
// Note that any library can be used as long as it is accepted by the ValidationAdaptor.
$validationProvider = new ValidationAdaptor($respectValidator);
$validationResultProcessor = new Helpers\ValidationResultProcessor($errorBag);
$rulesFactory = new Helpers\RulesFactory();
// Create the validation facade that will give us our validation object to work with.
$validationFacade = new ValidatorFacade(
$validationProvider,
$validationResultProcessor,
$rulesFactory
);
Validating data:, (*7)
<?php
// Prepare the input data.
$inputData = $_POST;
// Prepare rules to be applied on the input data.
$rules = [
# firstname and lastname must exists
# they should be alphanumeric
# atleast 2 characters
'firstname, lastname' => 'required|alpha|min:2',
# max until 18 characters only
'lastname' => 'max:18',
# must be an email format
# must be unique under 'users' table
'email' => 'email|unique:users',
# must be numeric
# must exists under 'users' table
'id' => 'numeric|exists:users',
'age' => 'min:16|numeric',
'info[country]' => 'required|alpha',
# roll[0] or roll[1] values must be in the middle 1 to 100
'roll[0], roll[1]' => 'numeric|between:1, 100',
# the format must be 'm-Y.d H:i'
'date' => 'dateFormat:(m-Y.d H:i)',
# it must be an image format under $_FILES global variable
'profileImg' => 'image',
# the provided phone number should follow the format
# correct: +38(123)456-12-34
# wrong: +38(123)56-123-56
# wrong: +39(123)456-12-34
'phoneMask' => 'phoneMask:(+38(###)###-##-##)',
'randNum' => 'between:1, 10|numeric',
# the value must be an IP Format
'ip' => 'ip',
'password' => 'required|min:6',
# the value from a key 'password' must be equal to 'password_repeat' value
'password_repeat' => 'same:password',
# it must be a json format
'json' => 'json',
'site' => 'url',
# cash10 or cash25 must only have these
# 1 or 2 or 5 or 10 or 20 or 50
'cash10, cash25' => 'in:1, 2, 5, 10, 20, 50',
# the value must not have 13 or 18 or 3 or 4
'elevatorFloor' => 'notIn:13, 18, 3, 4',
];
// Custom error messages for rules in case validation does not pass.
$customMessages = [
'info[country].alpha' => 'Only letters please',
'email.required' => 'Field :field: is required',
'email.email' => 'Email has bad format',
'email.unique' => 'This email :value: is not unique',
'elevatorFloor.notIn' => 'Oops',
];
// Run validation on input data.
$validationResult = $validationFacade->validate($inputData, $rules, $customMessages);
Methods available on the validationResult object:, (*8)
// Check if there are any errors.
$validationResult->hasErrors();
// Count number of errors.
$validationResult->count();
// Get all errors.
$validationResult->getErrors();
// Get error for a specific field.
$validationResult->getErrors('username');
// Get raw error messages with keys.
$validationResult->getRawErrors();
// Get only the first error message for each field.
$validationResult->firsts();
// Get the first error message for a specific field.
$validationResult->first('username');
// Appending an error message.
$validationResult->fieldsErrorBag->add($fieldName, $message);
Error message variables.
You can use 3 variables in your error messages, these will be dynamically replaced by the actual values being used at the time. These are:, (*9)
:field: => The field being validated.
:rule: => The rule being applied.
:value: => The value being validated against.
, (*10)
Contributing :octocat:
Dear contributors , the project is just started and it is not stable yet, we love to have your fork requests., (*11)
, (*12)
Testing
This project is mostly unit tested down to its core., (*13)
The testing suite can be run on your own machine. The main dependency is PHPUnit which can be installed using Composer:, (*14)
# run this command from project root
$ composer install --dev --prefer-source
vendor/bin/phpunit --configuration phpunit.xml --coverage-text
For additional information see PHPUnit The Command-Line Test Runner., (*15)
, (*16)
License
PHP Data Validator is open-sourced software licensed under the GNU GPL., (*17)