How It Works:
My set of scripts consist of two main files: a class that extends the original Smarty class and a validator function. Additionally, there are "validator files" which the other two files call on to preform various validation tasks.

Instead of using sessions to store validation data I decided to use flat files which remain the same for as long as the template is unchanged. This reduces the amount of file system access-- the same flat file is used for all users who access the page. Unlike other validation plugins that have to create a new session every time.

When enableValidate() is run from the script the precompiler function is registered as a Smarty prefilter. On first compliation of a template it checks for the text "validate" in the source. If it is found, the function creates a temp file containg the filename in an md5 hashsum. If no text is found, it unlinks any possible files with the same name (therefore the validation functions following it return true without performing anything).

The {validate} function set inside of the template takes its arguments and passes them into an array which is then serialized and saved into the temp file.

The doValidate() method runs through the temp file's unserialized array and runs each check specified. The checks are loaded from the "validators" directory found in the same location as this file. They are labeled in the follow manner: validator.[checkname].php As of this version, the name is case-sensative.

If a check does not go through, it returns false. The doValidate() method sets a flag for that particular field, signifiying an error. It itself then returns false.

At the same time, the {validate} function is re-run (since the form was sent back to the same page, this is the 2nd time {validate} is run). It checks an array inside of the Smarty object for any flags. If a flag was set, the message specific is echo'ed.

Installation:
* Drop function.validate.php into the Smarty 'plugins' directory.
* Put ValidatePHP.php into the same directory as Smarty.class.php.
* Create a directory named "validators" in the same directory and put each validator into there (use the filenames in the comments).
* Include both Smarty and "ValidatePHP.php" in your script and instead of calling "$tp = new Smarty;" use "$tp = new SmartyValidatePHP;" to initiate smarty.

Example:
Inside of the PHP file you post back to:

$tp->enableValidate();
if (count($_POST)>0 && $tp->doValidate()) {
   echo "success!"
}

And in the template:
<input type="text" name="frmName" />
{validate name="frmName" check="notEmpty" message="Name field cannot be empty!"}
 
