Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

Related Links

Expandable Form Validation Class : Part 2

pages (2): [1] 2


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 Part 1 of this tutorial, we covered the basics of our Form Validation Class.  We explained how the core of the Class depends on regular expressions and gave an insight regarding the keyword concept. We also explained how each keyword will have a corresponding Class Method so that we can easily extend the Class in the future to suit our needs.

In Part 2, we will get our hands dirty in creating the remaining useful Class Methods that will instruct the Class which form fields to validate and how to validate them.

In comparison to Part 1, this Part is significantly shorter, easier, and to the point.


Adding Fields To Be Validated
To use our Class in a development environment, we must somehow provide a way for the programmer to instruct the Class which form fields to validate upon submission.

This will be done via a new Class Method we will now create called addFormField( ).


The addFormField( ) Method
Our Method will accept two arguments, the name of the form field and the keywords for it. Let's look at the code and then explain each section.


<?php

    
function addFormField$fName $keyword )
    {
        
/**
         * Check If Form Is Sent.
         *
         * If the form is not sent, an error will always be triggered

         * because the Class will assume no form field exists.
         */
        
if( count$this->_f_data ) == )
        {
         
$this->_l_sError[] =    'addFormField()       Method : No Form Data : Ignore Add Field : ' .
                                 
'<strong>' $fName '</strong>';
            return;
        }

        
/**
         * Determine If Form Field Name Exists.
         * Adding a form field to the array is only done

         * if the form field exists. This is to avoid any
         * possible errors in the validation
         * process from occuring on a non existant field.
         * If a form field does not exist, a system error is triggered.
         */
        
if( array_key_exists$fName $this->_f_data ) == FALSE )
        {
         
$this->_l_sError[] =    'addFormField() Method : Non Existant Form Field : ' .
                                 
'<strong>' $fName '</strong>';
            return;
        }

        
// Add Keywords.

        
$this->_f_data$keyword $fName ] = $this->_f_data$fName ];

        
// Remove Form Field Name Without Keywords.

        
unset( $this->_f_data$fName ] );
    }

?>

In Part 1, we explained that all form data sent by PHP is either saved in a $POST or $GET Super Global. To make life easier for us, we save this data in a Class Attribute depending on the form method so we do not always have to check both Super Global's.

We want to add some sort of error check to our Class, to avoid adding non existent fields. This is to ensure that our Class will run at the fastest possible speeds. If non existent fields are allowed to be added, not only will PHP parser warning be generated, but our Class will loop through an endless number of form fields that simply do not exist.


<?php

        
if( count$this->_f_data ) == )
        {
            
$this->_l_sError[] =    'addFormField() Method : No Form Data : Ignore Add Field : ' .
                                    
'<strong>' $fName '</strong>';
            return;
        }

?>

In the above code segment, we make sure the form was submitted, there hence making sure that there is some sort of form data to work with. Technically we can skip the top step, since the next code segment also does some sort of error check, but leaving it won't hurt.


<?php

        
if( array_key_exists$fName $this->_f_data ) == FALSE )
        {
            
$this->_l_sError[] =    'addFormField() Method : Non Existant Form Field : ' .
                                    
'<strong>' $fName '</strong>';
            return;
        }

?>

Again here we make use of the PHP function array_key_exists( ). If you remember from Part 1, we said that the array housing our form data has as keys each form field name.

So by checking that there is a key with our form field name that we wish to add does indeed exist in our array, we make sure that we are not adding a non existent field. The above code segment returns a Class system error message if the key does not exist.

Having made sure that no errors will be encountered when adding a new field, we then proceed to add a new field to our array, with the keywords included. This is a two step process.

First we add to our array a new key with a value of the keywords followed by the field name. Then we remove the old key to avoid any errors.

What does this mean? Well, let's say in our array we had a key of Email and we wanted to add a keyword of (Required). We will create a new key in our array called (Required)Email then remove the old key of Email alone. This is exactly what we have done in the code below.


<?php

        
// Add Keywords.

        
$this->_f_data$keyword $fName ] = $this->_f_data$fName ];

        
// Remove Form Field Name Without Keywords.

        
unset( $this->_f_data$fName ] );

?>

- Tutorial written by Limitless

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

Shiny Spheres
Shiny Spheres
- Adobe Photoshop -
Isometric Pixel Art
Isometric Pixel Art
- Adobe Photoshop -
The Glamour Effect
The Glamour Effect
- Adobe Photoshop -
Sweat & Grease Shine
Sweat & Grease Shine
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 55 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 57 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 121 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 95 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 28 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 112 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 137 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 163 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 164 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 205 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415