romeoz / rock-validate
Flexible validator for PHP with I18N.
Installs: 1 673
Dependents: 6
Suggesters: 4
Security: 0
Stars: 25
Watchers: 5
Forks: 2
Open Issues: 1
Requires
- php: >=5.4.0
- romeoz/rock-base: ~0.12
- true/punycode: ~1.0
Requires (Dev)
- phpunit/phpunit: ^4.7.0
- romeoz/rock-date: ~0.12
README
Features
- Supports large many validation rules (string, number, ctype, file, network)
- Validation of scalar variable and array (
attributes()
) - Output the list of errors in an associative array
- i18n support
- Hot replacement of placeholders for messages (
{{name}} must be valid
), as well messages - Customization of validation rules
- Module for Rock Framework
Bolded features are different from Respect/Validation.
Table of Contents
Installation
From the Command Line:
composer require romeoz/rock-validate
In your composer.json:
{ "require": { "romeoz/rock-validate": "*" } }
Quick Start
use rock\validate\Validate; // Validation length from 10 to 20 characters inclusive + regexp pattern $v = Validate::length(10, 20, true)->regex('/^[a-z]+$/i'); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'value must have a length between 10 and 20', 'regex' => 'value contains invalid characters' ] */ $v->getFirstError(); // output: value must have a length between 10 and 20
####Replacement a placeholder
use rock\validate\Validate; $v = Validate::length(10, 20, true) ->regex('/^[a-z]+$/i') ->setPlaceholders(['name' => 'username']); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'username must have a length between 10 and 20', 'regex' => 'username contains invalid characters', ] */
####i18n
use rock\validate\Validate; $v = Validate::length(10, 20, true)->regex('/^[a-z]+$/i')->setLocale('ru'); $v->validate('O’Reilly'); // output: false $v->getErrors(); /* output: [ 'length' => 'значение должно иметь длину в диапазоне от 10 до 20', 'regex' => 'значение содержит неверные символы', ] */
####As Array or Object
use rock\validate\Validate; $input = [ 'username' => 'O’Reilly', 'email' => 'o-reilly@site' ]; $attributes = [ 'username' => Validate::required() ->length(10, 20, true) ->regex('/^[a-z]+$/i') ->setPlaceholders(['name' => 'username']), 'email' => Validate::required()->email() ]; $v = Validate::attributes($attributes); $v->validate($input); // output false $v->getErrors(); /* output: [ 'username' => [ 'length' => 'username must have a length between 10 and 20', 'regex' => 'username contains invalid characters', ], 'email' => [ 'email' => 'email must be valid email', ] ] */ $attribute = 'email'; $v->getFirstError($attribute); // output: email must be valid
Documentation
Demo
- Install Docker or askubuntu
docker run --name demo -d -p 8080:80 romeoz/docker-rock-validate
- Open demo http://localhost:8080/
Requirements
- PHP 5.4+
License
The Rock Validate is open-sourced software licensed under the MIT license.