Download the PHP package frangeris/simple-validation without Composer
On this page you can find all versions of the php package frangeris/simple-validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download frangeris/simple-validation
More information about frangeris/simple-validation
Files in frangeris/simple-validation
Package simple-validation
Short Description Simple, fast, beautiful way to make validations for forms or anything you can think of
License MIT
Homepage https://github.com/frangeris/simple-validation
Informations about the package simple-validation
Simple validation for PHP 5.5+
There are so many differents ways to make a validation for forms on php, each framework(Laravel 4+, Symfony 2+, CodeIgniter , FuelPHP, CakePHP, bla bla bla) has their own implementation on validations, ok, that's ok, it is expected that they supply us that funcionality, but, sometimes all those validations are complicated, sometimes we dont want to insert the rules inside a model when we just wanna validate a simple html form and get the possible failures, just simple like that, it ok that you insert your rules inside your model, this is another way just with super powers...
Installation
Easiest way would be using Composer and adding this to the composer.json requirement section.
It's PSR-0 compliant, so you can also use your own custom autoloader.
Usage
In general, every form could have rules, what is a rule?, rule is just what you want to validate, just like that, here is the list of rules we support:
- regex (for regular expression purposes)
- min (validate that is upper than)
- max (validate that don't exced a number)
- required (validate that the value is not null or empty)
- filter (php native filter :D)
- uploaded (validate that the file has been uploaded)
- file_max (validate the max size of the file using MB for comparation)
- allow_format (specify with formats are allowed for the file uploaded)
- not_equal (verify that the value is not equal to expressed pattern)
So let's rock and roll:
Here is an example of the structure for write your own validations, in this case I wanna validate a form for just save users over 18 years old, and of course a valid first name, last name and email.
file with rules:
As you can see frm_user_add
is the name of a form, followed by an associative array for the fields requirements and messages.
email
is a field inside the form frm_build_add
, we wanna that this field exist and must to be valid using the filters inside PHP, depending of each error, the string inside message is gonna be returned... Awesome right? :D
f_name
is a little complicated, is this case we want that the field be required, > 3 and < 30, and needs to match with the regular expression /^[a-z ]+$/i
...
For l_name
acts the same rule that f_name
...
And the last one age
, I just wanna that exist, and the minimun value must to be 18, and that's it... :)
In the PHP side just call the validator class and pass the fields on request and the name of the form:
This example is using phalcon framework making a POST request
The required function can be placed in another place, just remember that the isValid
method needs this array for validate...
PS: If you are using phalcon, dont do this in this way, create a DI inside your services and use it as di, someting like:
// Set up the validator service
$di->set('validator', function() use ($config)
{
$validations = require_once($config->security->validations);
return Simple\Validation\Validator::getInstance($validations);
});