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

How Things Work - 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.

  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


Now, before proceeding further, I need to decide how this class is going to work. Here's how I plan to use it:

<?php // some form processor // let's assume that the variables $a and $b // have been obtained via a form POST operation // create an object $fv = new FormValidator(); // run validation methods // the first argument is the name of the field to validate // the second is the error to report if validation fails $fv->isString("a", "Please enter a string for field A"); // same here $fv->isNumber("b", "Please enter a string for field B"); // check to see error status echo $fv->isError(); ?>
As you can see, I would like to call class methods with two parameters: the name of the form variable to validate, and the error message to return in the event the validation test fails. With this in mind, it becomes much easier to design the class appropriately.

Once the basic functionality of the class is clear, it's a good idea to spend some time listing the important methods, together with their purpose. Here's my initial cut:

isEmpty($var, $errormsg) - check whether the specified form variable is empty;

isString($var, $errormsg) - check whether the specified form variable contains string data;

isNumber($var, $errormsg) - check whether the specified form variable contains numeric data;

isWithinRange($var, $errormsg) - check whether the specified form variable contains a value within the specified numeric range;

isEmailAddress($var, $errormsg) - check whether the specified form variable contains a valid email address;

isError() - check whether any errors have occurred in form validation;

getErrorList() - return the list of validation errors;

resetErrorList() - reset the error list.

These are the essential methods; there may be more, which I will add as development progresses.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: