| 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
|

|
|
 |
php, shoutbox problems Author: vanhansen Posted: Nov 17th, 1:30am Activity: 5 replies, 93 views
|  | MarkupGeeks Logo Author: ahstanford Posted: Nov 16th, 8:45pm Activity: 12 replies, 161 views
|  | Drawing Tutorials Author: ahstanford Posted: Nov 16th, 12:46am Activity: 0 replies, 100 views
|  | Superbowl predictions, anyone? Author: ahstanford Posted: Nov 15th, 10:46pm Activity: 10 replies, 138 views
|  | Photomanipulation Footsteps Author: ahstanford Posted: Nov 15th, 10:43pm Activity: 4 replies, 102 views
|  | Learning to draw... Author: ahstanford Posted: Nov 15th, 12:43pm Activity: 4 replies, 116 views
|  | Looking for simple UI elements Author: FenixRoA Posted: Nov 15th, 6:40am Activity: 7 replies, 109 views
|  | HDD Help? Author: Phoenix Wynde Posted: Nov 13th, 2:31am Activity: 1 replies, 107 views
|  | Fun New Battles Posted! Author: ahstanford Posted: Nov 11th, 7:33pm Activity: 0 replies, 141 views
|  | 4-man Simon Tournament Author: ahstanford Posted: Nov 11th, 3:28pm Activity: 0 replies, 92 views
|  | Design Brief Inspiration for BioRUST Battles! Author: ahstanford Posted: Nov 11th, 7:19am Activity: 4 replies, 140 views
|  | The BioRUST Free Stock Photography Thread Author: ahstanford Posted: Nov 11th, 6:32am Activity: 2 replies, 148 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|