FormVal is a PHP class library for server-side form validation.

Demo

My webhost currently doesn't support PHP 5, so there's no online demo at the moment. An example, however, is included in the distribution.

Features

How to Use It

  1. Download FormVal and extract it somewhere in your web directory.

  2. Include FormVal.php on your page.

  3. In the section of your PHP code that handles the form post, instantiate a new FormValSet object. For example:

    $fv = new FormValSet();
  4. Use the object's add() method to add validation criteria. For example:

    $fv->add( new FormValEmail( 'email-field-name', 'The email address you entered is not valid.' ) );
  5. Optional: use the object's addError() method to add error messages for validation not supported by the library (such as checks against a database). Example:

    $fv->addError( 'The specified username does not exist.' );
  6. The validate() method processes all validators and returns true if the form is valid, or false if there are problems.

    If any errors were encountered, the reportErrors() method will return an HTML string explaining them to the user.

    For example:

    if ( $fv->validate() ) { // ...accept the form post (update the database, or whatever)... } else { echo $formval->reportErrors(); }

License

License details are included in the distribution, and are also available here.

Download

Comments? Suggestions?

Feel free to share your thoughts on this script. Suggestions for improvements or additional features are welcome.

Please don't feel free to bug me with implementation problems unless you're reasonably sure they're due to a flaw in the script itself, or an oversight in my instructions. Make sure you followed the instructions exactly. Make sure your pathnames are correct. Make sure you're using a supported version of PHP. &c.