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

Running On Empty - 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


Let's now begin constructing the validation routines themselves.

Here's the first one, which performs a very basic test - it checks to see whether the corresponding variable contains a value or not.

<?php class FormValidator { // snip // // methods (public) // // check whether input is empty function isEmpty($field, $msg) { $value = $this->_getValue($field); if (trim($value) == "") { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } // snip } ?>
This is fairly simple, but worth examining in detail, since all subsequent methods will follow a similar structure.

The isEmpty() public method is called with two arguments: the name of the form variable to be tested, and the error message to be displayed if it is found to be empty. This method internally calls the _getValue() method to obtain the value of the named form variable; it then trim()s this value and checks to see if it is empty.

If the variable is empty, a new element is added to the private class variable $_errorList (an array) to represent the error. This new element is itself a three-element associative array, with the keys "field", "value" and "msg", representing the form variable name, the current value of that variable, and the corresponding error message respectively.

If the variable is not empty, it will pass the test and the method will simply return true; this is handy in case you want to wrap the method call in a conditional test in your form processing script.

The reason for passing the form variable's name to the class method, rather than using its value directly, should also now be clear. When I pass the variable name to the method as a string, it can be used to access the corresponding form variable via the _getValue() method, and also becomes available within the class scope. This allows me to populate the $_errorList array with both the form variable name and its value; this additional information can come in handy when debugging long and complex forms.

If, instead, I had used the form variable's value directly, I would have certainly been able to record the incorrect form value in the $_errorList array, but would have no way to record the corresponding form variable name.

This might sound overly complicated, but it's actually fairly simple. In order to understand it better, try altering the class methods to accept form data directly, and you will see that it becomes much harder to record the name of the offending form variable(s) in the $_errorList array.

Finally, in case you're wondering, the $this prefix provides a convenient way to access variables and functions which are "local" to the class.

Let's move on to a couple of other, similar functions:

<?php class FormValidator { // snip // check whether input is a string function isString($field, $msg) { $value = $this->_getValue($field); if(!is_string($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } // check whether input is a number function isNumber($field, $msg) { $value = $this->_getValue($field); if(!is_numeric($value)) { $this->_errorList[] = array("field" => $field, "value" => $value, "msg" => $msg); return false; } else { return true; } } } ?>
These are very similar, simply using PHP's built-in variable functions to determine whether the data passed to them is numeric or character data.

>>> 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: