My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Wiki pages

BetterValidation

Simple clean validation library. Very new, wrote it after being both frustrated and inspired by Codeigniters validation library and PHP Validation by Benjamin Keen.

BetterValidation may sound a little conceited but I really only mean I built on some good ideas and made something that met my own requirements much better. Was only a working title but I couldn't think of anything else to name it and I wanted to offer it to the community so ...

Usage

The current usage is quite similar to both Codeigniter and PHP Validation. Unlike Benjamin Keen's implementation I have nether required or allowed error messages to be passed in with the rules. This and any HTML formatting is left to the rest of your program to handle as a validation class is not the right place for them.

Currently you would specify rules in an associated array:

$myrules = array('name'           => 'min_length[4]',
                 'username'       => 'required|min_length[4]|max_length[128]',
                 'password'       => 'required|min_length[4]',
                 'passconf'       => 'required|matches[password]',
                 'email'          => 'required|valid_email',
                 'account_number' => 'required|regex[/[\d]+\-[A-Z]{4}\-[\d]{2}/]');

Then pass the array to a new ValidationRules instance:

$rules = new ValidationRules($myrules);

You can then run the validation:

$rules->validate($data, false);

Here I have assumed you have an associated array of data in a variable called $data, something similar to the following:

$data = array('name'           => 'me',
              'username'       => 'myusername',
              'password'       => 'MyPåssw00rd',
              'passconf'       => 'MyPåssw00rd',
              'email'          => 'user@example.com',
              'account_number' => '1-ABCD-23');

This example will fail validation, here is what output you will get in order to handle your response to the user:

array(1) {
  ["name"]=>
  array(1) {
    [0]=>
    string(10) "min_length"
  }
Powered by Google Project Hosting