Welcome, Guest

Please login or register

TUTORIALS SUBMENU

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

Related Links

Expandable Form Validation Class : Part 2

pages (2): 1 [2]


A Little Explaining
Some people might argue that this way of adding form fields with keywords is not the best method. They state that another array could be created to house all keywords relevant to each form field. Then simply in the validation process, each array can be called for each form field to determine how to validate.

I disagree with this method for several reasons. First, this Class is designed to be fast an non memory intensive. Creating another array will just eat up more memory, especially if your form gets large enough. Imagine a multi page form with 30 fields, some of which have multiple selections such as check boxes and radio buttons.

To create a second array housing all possible keywords will mean an average of one key for each choice. Then to link that array with each form field will take a little extra processing when validation; we would have to loop through each form field to find its associated keyword, then loop through it again to validate each keyword.

If you remember our validate( ) Method from Part 1, this would mean 2 extra nested loops, brining the grand total to 4. Indeed a lot of extra unnecessary overhead.

Even if we are using Regular Expressions, because our form field names are short, will usually be no larger than 25 characters, the pattern to match is faster than all the overhead suggested above combined.

One array is simple code wise and much faster and more convenient.

 

Form Field Names Without Keywords
Because we want to display our form field names next to the generated error messages without the keywords included, we have a special Method that does so, which you will remember from Part 1. Let's see some code:


<?php

function _s_tFName( )
{
    
// Define Local Variables.

    
$l_dataCount    count$this->_f_data );
    
$l_nameStart    NULL;

    
// Loop.

    
for( $i 0$i $l_dataCount$i++ )
    {
        
// Find Last ')' Character In Field Name.

        
$l_nameStart strrpos$this->_f_cFName$i ] , ')' );

        
// Extract Field Name Without Keyword.

        
$this->_f_tFName[] = str_replace'_' ' ' substr$this->_f_cFName$i ] , $l_nameStart ) );
    }
}

?>

We loop through each form field finding the last closing parenthesis. If you remember from Part 1, our keywords are housed in ( ) and they precede the form field name. So by finding the last closing parenthesis, ), and extracting all characters after it, we get the form field name alone.


<?php

// Extract Field Name Without Keyword.

$this->_f_tFName[] = str_replace'_' ' ' substr$this->_f_cFName$i ] , $l_nameStart ) );

?>

Why are we using the str_replace( ) function? For form field names with a space character, " " included in it, PHP by default adds a _ where the space character is. That just looks plain ugly, so we simply remove the _ from the form field name when extracting it.

 

Returning Error Messages
We need to provide a way to return both form and system error messages. This is better than simply printing them because they can be passed as arguments to a format function, or be used anywhere in the code to be displayed where the user want them to be in the HTML layout. The code is pretty simple to understand and requires no further explaining.


<?php

    
function getFormError( ) {
        return 
$this->_l_fError;
    }

    
///////////////////////////
    
function getSystemError( ) {
        return 
$this->_l_sError;
    }

?>



Conclusion
In Part 2 here we covered how the Class can be used by the programmer and created several Class Methods to explain how to instruct the Class which form fields to validate as well as supported the methods we used.

In Part 3, we will create several validation Methods and take the Class on a test run to see how it works and how it can be used directly in a project.

A couple of closing notes; all our support was tested in actual environments and while the execution speed of PHP scripts will vary depending on the version used, server traffic, and connection speed, the results were pretty accurate over several tests. While this Class can be done in many different ways, we are happy with ours and we invite you, dear reader, to experiment and try other methods. After all the code is only as good as the programmer and we in no means are saying we gods. I am sure someone out there that is better than us can come up with a different method

- Tutorial written by Limitless

Pages (2): <Prev 1 [2]
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

Signature Techniques
Signature Techniques
- Adobe Photoshop -
Abstract Grunge In...
Abstract Grunge In...
- Adobe Photoshop -
Anti-Aliasing Inte...
Anti-Aliasing Inte...
- Adobe Photoshop -
The Glamour Effect
The Glamour Effect
- 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