Home arrow PHP arrow Page 10 - Building An Extensible Form Validator Class

A Quick Snack - PHP

Wondering what OOP can do for you? Well, wonder no more - thisarticle demonstrates how OOP can save you time and effort by building aPHP-based Form Validator object to validate HTML form input. In additionto a detailed walkthrough of the process of constructing a PHP class totest user input, this article also includes usage examples and a brieflook at some powerful open-source alternatives.

TABLE OF CONTENTS:
  1. Building An Extensible Form Validator Class
  2. Back To Class
  3. The Bare Bones
  4. How Things Work
  5. Private Eye
  6. Running On Empty
  7. Floating Like A Butterfly
  8. Mail Dot Com
  9. Under Construction
  10. A Quick Snack
  11. Going To The Source
  12. Closing Time
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 54
March 27, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
At this stage, I think I have enough building blocks to actually begin using this class to perform form input validation. Keep in mind, though, that I've been wrong before, and so my initial feeling of satisfaction may soon disappear.

First, let's build a simple form:

<html> <head> <basefont face="Arial"> </head> <body> <form action="processor.php" method="POST"> <b>Name:</b> <br> <input type="text" name="name" size="15"> <p> <b>Age:</b> <br> <input type="text" name="age" size="2" maxlength="2"> <p> <b>Sex:</b> <br> <input type="Radio" name="sex" value="m">Male <input type="Radio" name="sex" value="f">Female <p> <b>Favourite sandwich type:</b> <br> <select name="stype"> <option value="">-select one-</option> <option value="1">Thin crust</option> <option value="2">Thick crust</option> <option value="3">Toasted</option> </select> <p> <b>Favourite sandwich filling:</b> <br> <input type="Checkbox" name="sfill[]" value="BLT">Bacon, lettuce tomato <input type="Checkbox" name="sfill[]" value="EC">Egg and cheese <input type="Checkbox" name="sfill[]" value="PBJ">Peanut butter and jelly <p> <input type="Submit" name="submit" value="Save"> </form> </body> </html>
Here's what it looks like:



Now, when this form is submitted, the PHP script "processor.php" will take over. This script will first need to verify that the data entered into the form is acceptable (that's where our newly-minted FormValidator class comes in) and then do something with it (for example, INSERT the data into a MySQL database). Take a look:

<?php // include class include("FormValidator.class.inc"); // instantiate object $fv = new FormValidator(); // perform validation $fv->isEmpty("name", "Please enter a name"); $fv->isNumber("age", "Please enter a valid age"); $fv->isWithinRange("age", "Please enter an age within the numeric range 1-99", 1, 99); $fv->isEmpty("sex", "Please enter your sex"); $fv->isEmpty("stype", "Please select one of the listed sandwich types"); $fv->isEmpty("sfill", "Please select one or more of the listed sandwich fillings"); if ($fv->isError()) { $errors = $fv->getErrorList(); echo "<b>The operation could not be performed because one or more error(s) occurred.</b> <p> Please resubmit the form after making the following changes:"; echo "<ul>"; foreach ($errors as $e) { echo "<li>" . $e['msg']; } echo "</ul>"; } else { // do something useful with the data echo "Data OK"; } ?>
Let's see if it works. Here's what happens when I submit the form with no fields filled in:

The operation could not be performed because one or more error(s) occurred. Please resubmit the form after making the following changes: * Please enter a name * Please enter a valid age * Please enter an age within the numeric range 1-99 * Please enter your sex * Please select one of the listed sandwich types * Please select one or more of the listed sandwich fillings
And here's what happens when I submit the form with incorrect data in the age field:

The operation could not be performed because one or more error(s) occurred. Please resubmit the form after making the following changes: * Please enter an age within the numeric range 1-99
And here's what happens when I repent, correct all my errors and submit the form with valid data:

Data OK


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: