eclemens / php-validation
PHP Validation class inspired by the jQuery Validation Plugin
0.1.2
2015-10-06 03:47 UTC
Requires
- php: >=5.4
Requires (Dev)
This package is not auto-updated.
Last update: 2025-03-15 20:22:29 UTC
README
PHP Validation class inspired by the jQuery Validation Plugin (http://jqueryvalidation.org/)
Description
I created this library to use along with jQuery Validation Plugin.
It allows to use the same (or similar) rules used for jQuery Validation Plugin implementation in the client side to validate the request in the server side.
Installation
Install using composer. Exists as eclemens/php-validation in the packagist repository.
Add the package to the require section in your composer.json file:
{ "require" : { "eclemens/php-validation": "dev-master" } }
Usage
Basic standalone usage
PHP
<?php require_once __DIR__ . '/vendor/autoload.php'; // Create validator instance $validator = new PHPValidation\Validation(); // Add rules $validator->rules([ 'username' => ['required' => true, 'rangelength' => [3, 64]], 'email' => ['required' => true, 'email' => true], 'password' => ['required' => true], 'repassword' => ['equalTo' => 'password'] ]); // Data $data = [ 'username' => 'johndoe', 'email' => 'johndoe@example.org', 'password' => 'pass1234', 'repassword' => 'pass1234' ]; // Validate: if ($validator->valid($data)) { // It's a valid data! }
Usage along with jQuery Validation Plugin
HTML
<form> <input type="text" name="username"> <input type="email" name="email"> <input type="password" name="password"> <input type="repassword" name="repassword"> <input type="submit" name="submit"> </form>
JavaScript
$("form").validate({ "rules": { "username": {"required": true, "rangelength": [3, 64]}, "email": {"required": true, "email": true}, "password": {"required": true}, "repassword": {"equalTo": "[name=password]"} } });
PHP
<?php require_once __DIR__ . '/vendor/autoload.php'; // Create validator instance $validator = new PHPValidation\Validation(); // Add rules $validator->rules([ 'username' => ['required' => true, 'rangelength' => [3, 64]], 'email' => ['required' => true, 'email' => true], 'password' => ['required' => true], 'repassword' => ['equalTo' => 'password'], ]); // Validate: if ($validator->valid($_REQUEST)) { // It's a valid data! }
TODO
- Missing additional rules
- i18n