PHP
  Home arrow PHP arrow Page 6 - Building An Extensible Form Validator Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Building An Extensible Form Validator Class
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 44
    2002-03-27


    Table of Contents:
  • Building An Extensible Form Validator Class
  • Back To Class
  • The Bare Bones
  • How Things Work
  • Private Eye
  • Running On Empty
  • Floating Like A Butterfly
  • Mail Dot Com
  • Under Construction
  • A Quick Snack
  • Going To The Source
  • Closing Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building An Extensible Form Validator Class - Running On Empty
    ( Page 6 of 12 )

    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
     

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek