|
A Little Explaining
Some people might argue that this way of adding form fields with keywords is
not the best method. They state that another array could be created to house
all keywords relevant to each form field. Then simply in the validation
process, each array can be called for each form field to determine how to
validate.
I disagree with this method for several reasons. First, this Class is
designed to be fast an non memory intensive. Creating another array will
just eat up more memory, especially if your form gets large enough. Imagine
a multi page form with 30 fields, some of which have multiple selections
such as check boxes and radio buttons.
To create a second array housing all possible keywords will mean an average
of one key for each choice. Then to link that array with each form field
will take a little extra processing when validation; we would have to loop
through each form field to find its associated keyword, then loop through it
again to validate each keyword.
If you remember our validate( ) Method from
Part 1, this would mean 2
extra nested loops, brining the grand total to 4. Indeed a lot of extra
unnecessary overhead.
Even if we are using Regular Expressions, because our form field names are
short, will usually be no larger than 25 characters, the pattern to match is
faster than all the overhead suggested above combined.
One array is simple code wise and much faster and more convenient.
Form Field Names Without Keywords
Because we want to display our form field names next to the generated error
messages without the keywords included, we have a special Method that does
so, which you will remember from
Part 1. Let's
see some code:
<?php
function _s_tFName( )
{
// Define Local Variables.
$l_dataCount = count( $this->_f_data );
$l_nameStart = NULL;
// Loop.
for( $i = 0; $i < $l_dataCount; $i++ )
{
// Find Last ')' Character In Field Name.
$l_nameStart = strrpos( $this->_f_cFName[ $i ] , ')' );
// Extract Field Name Without Keyword.
$this->_f_tFName[] = str_replace( '_' , ' ' , substr( $this->_f_cFName[ $i ] ,
$l_nameStart + 1 ) );
}
}
?>
|
We loop through each form field finding the last closing parenthesis. If you
remember from Part 1, our keywords are housed in ( ) and they precede the
form field name. So by finding the last closing parenthesis, ), and
extracting all characters after it, we get the form field name alone.
<?php
// Extract Field Name Without Keyword.
$this->_f_tFName[] = str_replace( '_' , ' ' , substr( $this->_f_cFName[ $i ] , $l_nameStart + 1 ) );
?>
|
Why are we using the str_replace( ) function? For form field names with a
space character, " " included in it, PHP by default adds a _ where the space
character is. That just looks plain ugly, so we simply remove the _ from the
form field name when extracting it.
Returning Error Messages
We need to provide a way to return both form and system error messages. This
is better than simply printing them because they can be passed as arguments
to a format function, or be used anywhere in the code to be displayed where
the user want them to be in the HTML layout. The code is pretty simple to
understand and requires no further explaining.
<?php
function getFormError( ) {
return $this->_l_fError;
}
/////////////////////////// function getSystemError( ) {
return $this->_l_sError;
}
?>
|
Conclusion
In Part 2 here we covered how the Class can be used by the programmer
and created several Class Methods to explain how to instruct the Class which
form fields to validate as well as supported the methods we used.
In Part 3, we will create several validation Methods and take the Class on a
test run to see how it works and how it can be used directly in a project.
A couple of closing notes; all our support was tested in actual environments and
while the execution speed of PHP scripts will vary depending on the version
used, server traffic, and connection speed, the results were pretty accurate
over several tests. While this Class can be done in many different ways, we are
happy with ours and we invite you, dear reader, to experiment and try other
methods. After all the code is only as good as the programmer and we in no means
are saying we gods. I am sure someone out there that is better than us can come
up with a different method
- Tutorial written by Limitless
|