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


The Validation Method

<?php

    
/**
     * 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 errors.
         * Return TRUE means that every field has been validated
         * correctly and it is now generally safe to use this form data.
         */
        
return TRUE;
    }

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

?>

This Method is the heart of our Class. Its main job is to loop through every form field, determine if there are any keywords associated with it and call the appropriate validation Method for each keyword.

Remember that we said that, for expansion reasons, each keyword will have its own validation Method. What this Method does is that it matches any keyword with the validation Method of the same name. If no Method exists, then it assumes an invalid keyword was associated with the field and will trigger a system error.

Keywords for our Class are detected as any text between parenthesis, ( ). So in order for this Class to work no parenthesis can be used in the form field name. If any text between parenthesis is found and does not have an associated validation Method, a system error will be triggered.

You might now be wondering how a keyword is matched with a validation Method. All validation Methods have the prefix _vm_ followed by the keyword in lowercase letters.

Consider the keyword Required for example. Its validation Method will be called _vm_required.

<?php

        
// Define Local Variables.

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

?>

What we do here is define the variables we will use within the Method.

$l_dataCount is simply the number of form fields. This is used so that we can loop through each field. You will remember that the Attribute $this->_f_data is used to house the form fields along with their values based on the submission Method.

$l_key is an array that will house all keywords for the current field in the loop.

$l_keyCount is simply the number of keywords found for the current field. It is used to start a nested loop to loop through each keyword found and call the appropriate validation Method.

$l_vm is where the fun really started. PHP allows for dynamic function or Method calls from string variables if the value of that string variable matches the name of a function or a Method. $l_vm will include the value of the Validation Method based on the keyword found so that it can be called. This will become more clear in the code that follows.

What do we mean by dynamic function calls? Consider the PHP native function count. If we save a string variable as follows:

We created a string variable with the value of count, which also happens to be the same name as a PHP native function. If we were to use the variable as a function, PHP will consider it the same as calling count( ). Take a look at the example to understand :

<?php

$dynamicCall 
'count';

// Call Function Count.
// Same As Writing count( $myArray ).

$dynamicCall$myArray );

// Same As Above.

count$myArray );

?>

- Tutorial written by Limitless

Pages (4): <Prev 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

Framing Your Paint...
Framing Your Paint...
- Maxon Cinema 4D -
Whispy Smoke
Whispy Smoke
- Adobe Photoshop -
Animated Movie-Cli...
Animated Movie-Cli...
- Adobe Photoshop -
StarBurst Effects
StarBurst Effects
- 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