Description

phalidate is a collection of PHP classes that handle data validation. The validation can be done on data that is coming from a form, or from another source (e.g. database). Currently there are two classes in phalidate, they are phalidate.php and phalidators.php. phalidate.php handles the logic of the validation process, tracking error messages and so on. phalidators.php is the collection of regular expressions and related checks used to validate specific types of data.

phalidate is designed to be very flexible and easy to use. It can be used with templating systems like Smarty, it can be easily extended, and it supports error messages in different languages.

Note: phalidate does not modify the data. It's a good plan to clean your data before validating it. To that end I'm working on another class, MrClean, which will handle most of the known problems with dirty data.

Features

* extensible design
* multiple field types can be validated
* can test a single field or a collection of fields
* currently contains 14 different validation types
* includes test suite
* includes example code

Documentation

Easy to use like most classes. Simply include the classes. Instantiate phalidators and start validating. The following code shows including and instantiating the classes.

<?php
require ("../phalidate.php");
require ("../phalidators.php");
$p = new phalidators();
.....
?>


Then we do some validation by calling the method that is specific to the data type and the kind of test we're testing for.

$p->is_filled_in("first_name",$_POST["first_name"]);
$p->is_alpha("first_name",$_POST["first_name"]);
$p->is_alphanumeric("alphanumeric",$_POST["alphanumeric"]);
$p->is_numeric("numeric",$_POST["numeric"]);
$p->is_date("date",$_POST["date"],1);
$p->is_email("email",$_POST["email"]);
$p->is_loosestring("textarea",$data["textarea"]);

Now we've done the validation, we need to get the errors (if there are any). There are three functions to use:
* get_errors_count(), returns a count of the number of errors,
* get_errors_id(), returns an array of the errors, with fieldname as the key and the error id as the value,
* get_errors_message(), returns an array of the errors, with the fieldname, error id and error message (in configurable language).

$errors = $p->get_errors_id();
or
$errors = $p->get_errors_message();
or
$errors = $p->get_errors_count();

In the first two examples, $errors is an array of the errors detected by phalidate. If you construct your form carefully, you can use the error messages from phalidate as the errors you send back to your users.