PHP
  Home arrow PHP arrow Page 2 - Building a PHP5 Form Processor: Coding the Form Validator Module
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? 
PHP

Building a PHP5 Form Processor: Coding the Form Validator Module
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-01-23


    Table of Contents:
  • Building a PHP5 Form Processor: Coding the Form Validator Module
  • Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
  • Testing numeric values and ranges: defining the “validateNumber()” and “validateRange()” methods
  • The big challenge: checking email addresses with the “validateEmail()” method
  • Checking for errors: defining the “checkErrors()” and displayErrors()” methods

  • 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 a PHP5 Form Processor: Coding the Form Validator Module - Checking for empty and integer values: looking at the “validateEmpty()” and “validateInteger()” methods
    ( Page 2 of 5 )

    My round trip for defining the checking methods of the class begins with the specific implementation of the “validateEmpty()” method, which can be used to quickly check whether a form field is empty or not. Below is the pertinent method definition:

    public function validateEmpty
    ($field,$errorMessage,$min=4,$max=32){
        if(!isset($_POST[$field])||trim($_POST[$field])==''||strlen
    ($_POST[$field])<$min||strlen($_POST[$field])>$max){
            $this->errors[]=$errorMessage;
        }
    }

    While the above method performs a rather trivial checking process, such as validating empty fields, it’s worth examining in detail, since all the subsequent methods will stick to a similar approach.

    As you’ve seen, this method is called with two arguments: the name of the form field to be tested, and the error message to be displayed in case the field is found to be empty. Besides the mandatory parameters, the method takes two additional arguments defined with default values, $min and $max. These arguments come in hand for checking whether values are within a given length of characters.

    Now, by returning to the validation process itself, notice how the PHP $_POST array is used internally, in order to access form variables. In this case, I’ve purposely utilized this PHP proprietary array, simply because I’m assuming that the form will use this request method. However, if you’re going to build your forms by using a specific request method, you may want to either hard code the proper $_POST /$_GET array for accessing form variables, or (a much better approximation) write a class method for obtaining the method used on the form.

    With reference to this method, if the field is empty, then the corresponding error message passed in as an argument is stored as a new element in the $errors array, so it can be accessed at a later time, after the form has been submitted.

    Since all of the checking process is extremely easy to follow, and the mechanism for storing error messages consists of a simple array, I can move forward to explain another validation method. In particular, this one is responsible for verifying whether a field contains an integer value. Its signature is listed below:

    public function validateInt($field,$errorMessage){
        if(!isset($_POST[$field])||!is_numeric($_POST[$field])
    ||intval($_POST[$field])!=$_POST[$field]){
            $this->errors[]=$errorMessage;
        }
    }

    As you can appreciate, the above method looks a little more interesting. In short, it will test whether the value entered in the field is an integer. In order to check for this condition, the method uses the powerful combination of the “is_numeric()” and “intval()” PHP functions respectively. As with the first method, if the value entered isn’t an integer, the appropriate error message is added as a new element to the $errors array. Simple and efficient.

    Now that you have a general idea of how most of the class methods work, it’s time to move on and define a few more methods. These are useful for testing whether the data is within a given range of numeric values, and eventually whether it’s numeric or not. So, scroll down the page and keep reading.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT