| Expandable Form Validation Class : Part 2 |
pages (2): 1 [2] |
|
|
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
| 
There are no comments for this tutorial yet. You can place a comment by clicking here.
|

|
|
 |
TrentonCS Author: ahstanford Posted: Nov 06th, 7:40pm Activity: 3 replies, 27 views
|  | Html 5 Author: ahstanford Posted: Nov 05th, 1:32pm Activity: 5 replies, 89 views
|  | What are your favorite websites? Author: ahstanford Posted: Nov 05th, 12:51am Activity: 0 replies, 47 views
|  | What do you do for a living? Author: ahstanford Posted: Nov 04th, 11:04pm Activity: 0 replies, 45 views
|  | What is your favorite Subway sandwich? Author: ahstanford Posted: Nov 04th, 11:02pm Activity: 4 replies, 88 views
|  | Windows 7 Author: ahstanford Posted: Nov 04th, 10:59pm Activity: 0 replies, 49 views
|  | Google Wave Author: ahstanford Posted: Nov 04th, 10:52pm Activity: 0 replies, 60 views
|  | University Project Author: Gjbphp Posted: Nov 03rd, 8:59pm Activity: 1 replies, 90 views
|  | Hello BioRust! Author: ahstanford Posted: Nov 02nd, 5:39pm Activity: 4 replies, 87 views
|  | Illustrator cs4 - Convert outlines/graphics to ... Author: izidrew Posted: Oct 29th, 3:48pm Activity: 3 replies, 259 views
|  | Hello BioRust!! Author: MOST Posted: Oct 29th, 12:52am Activity: 4 replies, 144 views
|  | Hey! newbie Author: prelimiting Posted: Oct 28th, 11:51pm Activity: 1 replies, 113 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|