<?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 , 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>';
}
}
}
?>
|
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 , 0 );
?>
|
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[ 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>';
}
}
?>
|
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
|