Welcome, Guest

Please login or register

TUTORIALS SUBMENU

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

Sponsored 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

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Meteor Tutorial - ...
Meteor Tutorial - ...
- Maxon Cinema 4D -
Hard-Edged Cel-Sha...
Hard-Edged Cel-Sha...
- Adobe Photoshop -
Hershey Kisses
Hershey Kisses
- Adobe Illustrator -
Thick Diagonal Lines
Thick Diagonal Lines
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Advertisements





Special Options
Printer Friendly Version
Forum Threads

 Re: 3ds Max Tutorials for Beginners
Author: 3DSMaxresources
Posted: Feb 22nd, 4:29pm
Activity: 0 replies, 855 views
Delete Account
Author: Neo824
Posted: Oct 18th, 7:47am
Activity: 1 replies, 1921 views
Back...
Author: unleash
Posted: Jul 02nd, 12:37pm
Activity: 2 replies, 2033 views
Help Please :)
Author: Roosta
Posted: Mar 25th, 5:08am
Activity: 0 replies, 2480 views
thank you
Author: HypepapyHer
Posted: Mar 24th, 9:18pm
Activity: 1 replies, 1801 views
 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 12:16pm
Activity: 1 replies, 2554 views
 changes....
Author: supertackyman
Posted: Sep 12th, 3:56am
Activity: 2 replies, 3534 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 4:50pm
Activity: 0 replies, 2720 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 1:58pm
Activity: 0 replies, 4089 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 3:18pm
Activity: 1 replies, 3874 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 3:33pm
Activity: 0 replies, 17228 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 4:37am
Activity: 0 replies, 4971 views
Forum Threads

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