Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Expandable Form Validation Class : Part 1

pages (4): [1] 2 3 4


IMPORTANT: This tutorial (and other component parts) make use of the datLib library, which you can download here. The author has stopped working on this project, but you can check out their website for similar up-to-date libraries.

In this rather advanced and sequential tutorial, we will teach you how to create an expandable Form Validation Class, that once complete, you will able to use in any of your PHP scripts. The beauty of this Class is that it is expandable with ease. This means that once the base code of the Class is complete, you can easily configure it to validate different types of form data with virtually no changes. The extra configurations will only be limited to your programming knowledge.

This tutorial is rather long and thus will be divided into several Parts. In Part 1 here, we will give an overview of the Class and how it will be used. Please make sure you have an idea of how to program in PHP as well as an idea on Object Oriented Programming. These concepts are not discussed here and even though we will try to be as descriptive as possible, an idea will help you understand what we are exactly talking about.

The ideas discussed in this tutorial are basically the same as the ones that our script, datVForm, is based on. So after you complete this tutorial, you might want to download datVForm to give it a shot and build upon it to test your know skills. Okay enough introductions, let's get started...


The Idea Behind The Code
There are literally hundreds, if not thousands, of tutorials available that teach you how to validate form data. This is important to ensure the security of your script and also to ensure that any user received data is in the format you want it to be in. As you develop different web sites, however, it becomes a big pain to copy the same code over and over again, modifying it to fit the new web site's needs.

Even if you wrap your form validation in functions, what happens when you want to expand on them? You have to rewrite them. And what about the error messages for each validation? These are all common features that with object oriented programming can become easier.


The Keyword Concept
Our Class will operate on a keyword basis. This means that based on a specific keyword we assign to each form field, the Class will know how to validate it. This makes the validation process very fast because the Class only validates fields with a keyword and nothing else. Secondly, it makes expanding the Class quite easy. To create new validation Methods, all you have to do is add a new keyword and tell the Class how to validate that keyword. Very simple.

A third concept is that because the validation always takes place on the server, the form will always be validated no matter where it is sent, from your server, or from a clients computer. In our script, we always instruct the Class to expect specific form fields and keywords. If such fields are not present, then it will assume the form has been tampered with and will always return an error. If the form field exists, it will always be validated!  Our Class will also be able to validate form data received by either a _POST or _GET method, which makes it much more portable.


Class Common Features
Our Class will have common features as well as several Methods. Let's first discuss the common features. First, there are the Form Error Messages and the System Error Messages.

The Form Error Messages are the error messages that are generated if a form field does not pass validation. So for example, if a form field is suppose to be an email and a user did not supply a valid email, this would generate a Form Error Message.

The System Error Messages are those that are generated by the Class when it encounters an internal error, such as a non existent keyword or non existent form field. These messages are helpful in the development process but should not be displayed when the Class is eventually used in a working environment.

The second set of common features are the Methods. Each Method in our Class will correspond to a keyword. Like stated before, a keyword instructs our Class to validate a form field in a specific way. This is done by calling the Method that corresponds to each keyword. Adding a new validation method is as simple as adding a new keyword and the Class Method that corresponds to it.


Creating The Class
Let's get started in creating our Class. The base code of our Class will contain several Attributes, a Constructor, and one Method. This one Method will be in charge of actually finding all form fields, determining if there are keywords associated with it, and calling the appropriate validation Method.

Because it is usually easier to see the code and then explain it, we will provide the code for the above elements that proceed to explain them one section a time. You should get an idea though of what is going on by reading the comments.

Please keep in mind that the code we show in this Part of the tutorial is not the complete Class, bur rather the important segments relevant to this Part. As Parts 2 and 3 become available, the code will be expanded to be much bigger.

<?php

class datVForm
{
    
/**
     * Form field names including the keywords.
     *
     * @var array
     */
    
var $_f_cFName;
    
/**
     * Form data.
     *
     * @var array
     */
    
var $_f_data;
    
/**
     * Form field names without the keywords.
     *
     * @var array
     */
    
var $_f_tFName;
    
/**
     * Form method.
     *
     * @var string
     */
    
var $_f_type;

    
/**
     * Form validation error messages.
     *
     * @var array
     */
    
var $_l_fError;
    
/**
     * Class system error messages.
     *
     * @var array
     */
    
var $_l_sError;

    
//////////////////////////////////////////////////////////////////////

    /**
     * Constructor.
     *
     * The Constructor has several jobs. First based on the form
     * method type, POST or GET, it will get all form data from
     * the PHP Super Global. Also it defines the attributes that
     * will be used by the Class.
     */
    
function datVForm$f_type )
    {
        
// Initialize Class Variables.

        
$this->_f_cFName    = array( );
        
$this->_f_data      = array( );
        
$this->_f_tFName    = array( );
        
$this->_f_type      $f_type;
        
$this->_l_fError    = array( );
        
$this->_l_sError    = array( );

        
// Determine Form Method.

        
if( $this->_f_type == 'p' ) {
            
$this->_f_data datLib::string_trim_array$_POST );
        }
        else if( 
$this->_f_type == 'g' ) {
            
$this->_f_data datLib::string_trim_array$_GET );
        }
        else {
            
$this->_l_sError[] =    'datVForm() Method : Invalid Form ' .
                                    
'Method :: Ignore Validation : <em>' .
                                    
'All Fields</em>';
        }
    }

    
//////////////////////////////////////////////////////////////////////

    /**
     * Validate.
     *
     * This Method does several things. First it determines how many form fields
     * there are in the submitted form, then loops through each and every single
     * one of them to determine which keywords, if any, are assosiated with one.
     *
     * If there are keywords assosiated with a field, it calls the appropiate
     * validation Method for that field.
     */
    
function validate( )
    {
        
// Define Local Variables.

        
$l_dataCount    count$this->_f_data );
        
$l_key          = array( );
        
$l_keyCount     NULL;
        
$l_vm             NULL;

        
// Find Complete Field Names.

        
$this->_f_cFName array_keys$this->_f_data );

        
// Find Field Names Without Keywords.

        
$this->_s_tFName( );

        
// Loop.

        
for( $i 0$i $l_dataCount$i++ )
        {
            
// Determine If Keyword Exists In Field Name.

            
preg_match_all'/\((.*?)\)/s' $this->_f_cFName$i ] , $l_key );

            
// Determine Number Of Keywords, If Any.

            
$l_keyCount datLib::array_count_2D$l_key );

            
// Loop.

            
for( $j 0$j $l_keyCount$j++ )
            {
   if( 
method_exists$this '_vm_' strtolower$l_key][ $j ] ) ) == TRUE )
                {
                    
// Extract Keyword Validation Method.

                    
$l_vm '_vm_' strtolower$l_key][ $j ] );

                    
// Call Validation Method.

                    
$this->$l_vm$this->_f_data$this->_f_cFName$i ] ] , $i );
                }
                else {
        
$this->_l_sError[] =    'validate() Method : Non Existant Keyword : ' .
               
'Ignore Validation : ' .
               
'<strong>' $this->_f_tFName$i ] . '</strong>';
                }
            }
        }

        
// Return.

        
if( datLib::array_isNull$this->_l_fError ) == FALSE ) {
            return 
FALSE;
        }

        if ( 
datLib::array_isNull$this->_l_sError ) == FALSE ) {
            return 
FALSE;
        }

        
/**
         * Return TRUE.
         *
         * The Method will only return TRUE if there are no validation errros.
         * Return TRUE means that every field has been validated
         * correctly and it is now generally safe to use this form data.
         */
        
return TRUE;
    }

    
//////////////////////////////////////////////////////////////////////
}

?>

That was a lot of code, so let's break up the code into segments and explain each segment alone...

- Tutorial written by Limitless

Pages (4): [1] 2 3 4 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  LimitLess (#32221)
Date: Sun Sep 24, 2006. 20:42:12

Post #2 of 2

Hello,

The download link for "datVForm" was taken offline because we are rewritting version 2 of the libary which is designed from the ground up to take advantage of PHP 5's object model as well as make expansion a little more easier for developers.

This of course will also mean that validation will execute so much faster. I'll let Man1c know when we release version 2 of "datVForm" so that all that benefited from this tutorial can download and enjoy it.

Reply to this post


User:  cool5353 (#31788)
Date: Sun Sep 10, 2006. 10:35:46

Post #1 of 2

I cannot locate a download link for the "datVform" which is mentioned in the article. The Limitless Studios site does not provide the link. Is this source code available elswhere ?

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

Turbulent Water
Turbulent Water
- Adobe Photoshop -
Diamond Plate Metal
Diamond Plate Metal
- Adobe Photoshop -
LCD Interfaces
LCD Interfaces
- Adobe Photoshop -
Fixing the PNG Col...
Fixing the PNG Col...
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673