PHP
  Home arrow PHP arrow Page 9 - Building An Extensible Form Validator ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Building An Extensible Form Validator Class
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 41
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Under Construction


    (Page 9 of 12 )

    So that takes care of the basic validation routines. As you can see, it's fairly easy to add new ones to the class, as per your specific requirements. All that's left now are a couple of methods to access the error list.

    The first of these is a simple little method named isError(), which lets you know whether or not any errors have occurred yet. Internally, all this method does is check the size of the $_errorList array; if the size of the array is greater than 1, it implies that one or more errors have occurred while validating the form data. Take a look:

    <?php class FormValidator { // snip // check whether any errors have occurred in validation // returns Boolean function isError() { if (sizeof($this->_errorList) > 0) { return true; } else { return false; } } } ?>
    Of course, all isError() does is tell you whether or not an error occurred; it won't let you view the list of errors. For that, you need the getErrorList() method, which returns the current $_errorList array.

    <?php class FormValidator { // snip // return the current list of errors function getErrorList() { return $this->_errorList; } } ?>
    Short and sweet!

    Finally, how about resetting the error list? Well, that's why I have the resetErrorList() method, which clears the $_errorList array of all data.

    <?php class FormValidator { // snip // reset the error list function resetErrorList() { $this->_errorList = array(); } } ?>
    It's a good idea to run this resetErrorList() method whenever the class is first instantiated - which is why I've added it to the class constructor.

    <?php class FormValidator { // snip // constructor // reset error list function FormValidator() { $this->resetErrorList(); } } ?>
    In case you didn't already know, PHP makes it possible to automatically execute a specific function when a new instance of a class is spawned. This function is referred to as a "constructor" and must have the same name as the class. Which, if you look at the code above, is exactly what I've done.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT