| 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 , 0 );
// Loop.
for( $j = 0; $j < $l_keyCount; $j++ )
{
if( method_exists( $this , '_vm_' . strtolower( $l_key[ 1 ][ $j ] ) ) == TRUE )
{
// Extract Keyword Validation Method.
$l_vm = '_vm_' . strtolower( $l_key[ 1 ][ $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
|

|
|
 |
Help Author: kitty.tina Posted: Nov 19th, 7:48pm Activity: 2 replies, 37 views
|  | Zen Author: hackzenindus Posted: Nov 19th, 2:54pm Activity: 6 replies, 81 views
|  | Submit/sell/show my designs Author: lakotaswift@hot Posted: Nov 19th, 12:38pm Activity: 1 replies, 30 views
|  | Are the Tutorials available??? Author: LivingCovers Posted: Nov 18th, 10:15am Activity: 2 replies, 48 views
|  | Graphic Artist from Nigeria, Africa... Author: LivingCovers Posted: Nov 18th, 8:47am Activity: 3 replies, 43 views
|  | New guy in VA Author: Rohape Posted: Nov 18th, 1:53am Activity: 4 replies, 52 views
|  | Doseages and Solutions Author: Guardian Posted: Nov 17th, 11:54pm Activity: 7 replies, 78 views
|  | Removing background from images Author: Alex Ross Posted: Nov 17th, 3:50pm Activity: 13 replies, 244 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 210 |
| Total Downloads: | 413 |
| Linkbase Links: | 243 |
 |
|
 |
 |
|