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]


<?php
        
// Find Complete Field Names.

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

        
// Find Field Names Without Keywords.

        
$this->_s_tFName( );

?>

Okay what we do here is get the form field names with the keywords and without the keywords. The _s_tFName( ) Method will be discussed in Part 2 of this tutorial, but for now just know that it extracts each form field name without the keywords included.

Let's now discuss the PHP native function array_keys( ). This function simply returns all the keys of an array. Remember that when a form is saved in the PHP Super Global POST or GET, it is saved in the format of $_POST[ key ] = value. In this case, each key in the Super Global is the name of our fields and each value is the value entered by the User for that field!.

So by calling the function array_keys we are returning the name of our form field names only! So simple to understand and follow.

<?php
        
// 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>';
                }
            }
        }

?>

Over in this code segment, what we are doing is that we are looping through each form field. That is what the first For Loop is for. Inside it we do several things to extract the keywords for each field.

<?php

            
// 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 );

?>

We call the PHP function preg_match_all to match any characters inside parenthesis, ( ). Like stated before, any keyword in our form field will be enclosed in them.

We then use the datLib Method array_count_2D to determine how many actual keywords are found. The preg_match_all function returns a 2D array that is why we do not use the native PHP function count. This is so we can loop through each keyword through a second nested For Loop.

The way preg_match_all works is that it finds any characters inside the given pattern. The first dimension in the array returned, $l_key are the matches including the pattern, which in our case is the parenthesis. The second dimension in the array are the matches alone, which in this case would be the actual keywords.

We need the keywords alone so that we can use them in our dynamic call to their Method. That is why further down in the code, when we reference to the matches array, we always reference to it from the second dimension.

<?php
            
// 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>';
                }
            }

?>

In the second nested For Loop, we loop through each found keyword. We first check that a Method for the keyword found exists to avoid any possible errors. If a Method does exist, we save the entire keyword, with _vm_ appended to it at the start in a variable called $l_vm to get the entire Method Name.

We then dynamically call the Method. If the Method does not exist, then a system error will be triggered to give us a proper warning.

Some things to pay close attention to in the last code segment. First, each validation Method is executed as soon as its associated keyword is found and in the order that they are found. Second, and most importantly, each validation Method is called with two arguments. This will be discussed in Part 3 of this tutorial, but lets give a little heads up here why. Each validation Method will be responsible for generating its own error messages. Thus it will need two things. The name of the field it is validation so that it can optionally include the form field name in the generated error message and the current position of the field in the array that includes the form field name without the keywords.

Each validation Method will obviously do some test on the form field name. Because the form field names are stored in an Attribute that includes the keywords, the first argument is required. A second Attribute, as you remember, also includes the form field names but without the keywords. The array is ordered in the order the fields are in the form.

Because the first For Loop also loops through each form field in the order it appears in the form, thus the current position of the For Loop will also provide an indicator as to the current form field's name without the keywords in the array.

<?php

        
// 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;
    }

?>

After each form field is looped through and validated, the Method will only return TRUE if there are no system or validation errors. If you remember, each validation Method generates its own error message. This error message should be saved in the form error Attribute so that it can later be displayed to the user in the place where you decide to display it.

Returning TRUE means that the form data is generally safe to use or is in the format that you want it to be. Thus it can then be used in your script.



Conclusion
In this Part of the tutorial, we have an overview of our Class, how it operates and how it can be expanded. We also gave an overview of key PHP features such as dynamic function calling and regular expressions.

In Part 2, we will discuss the remaining portions of our Class such as displaying the validation Method generated error messages as well as the system error messages.

In Part 3, we will discuss how to create several example validation Methods as well as put the Class into an example test to showcase our creation. We will also provide ideas on how the Class can be expanded or improved and some more ideas.

- Tutorial written by Limitless

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

StarBurst Effects
StarBurst Effects
- Adobe Photoshop -
Rotated Characters
Rotated Characters
- Adobe Illustrator -
Photo Frames
Photo Frames
- Adobe Illustrator -
Realistic Metal
Realistic Metal
- 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