| Expandable Form Validation Class : Part 2 |
pages (2): [1] 2 |
|
|
IMPORTANT: This tutorial (and other component parts) make use of the datLib library, which you can download here. The author has stopped working on this project, but you can check out their website for similar up-to-date libraries.
|
In Part 1 of this
tutorial, we covered the basics of our Form Validation Class. We explained how
the core of the Class depends on regular expressions and gave an insight
regarding the keyword concept. We also explained how each keyword will have a
corresponding Class Method so that we can easily extend the Class in the future
to suit our needs.
In Part 2, we will get our hands dirty in creating the remaining useful
Class Methods that will instruct the Class which form fields to validate and how
to validate them.
In comparison to Part
1, this Part is significantly shorter, easier, and to the point.
Adding Fields To Be Validated
To use our Class in a development environment, we must somehow provide a way for
the programmer to instruct the Class which form fields to validate upon
submission.
This will be done via a new Class Method we will now create called
addFormField( ).
The addFormField( ) Method
Our Method will accept two arguments, the name of the form field and the
keywords for it. Let's look at the code and then explain each section.
<?php
function addFormField( $fName , $keyword )
{
/**
* Check If Form Is Sent.
*
* If the form is not sent, an error will always be triggered
* because the Class will assume no form field exists.
*/
if( count( $this->_f_data ) == 0 )
{
$this->_l_sError[] = 'addFormField() Method : No Form Data : Ignore Add Field : ' .
'<strong>' . $fName . '</strong>';
return;
}
/**
* Determine If Form Field Name Exists.
*
Adding a form field to the array is only done
*
if the form field exists. This is to avoid any
*
possible errors in the validation
*
process from occuring on a non existant field.
* If a form field does not exist, a system error is triggered.
*/
if( array_key_exists( $fName , $this->_f_data ) == FALSE )
{
$this->_l_sError[] = 'addFormField() Method : Non Existant Form Field : ' .
'<strong>' . $fName . '</strong>';
return;
}
// Add Keywords.
$this->_f_data[ $keyword . $fName ] = $this->_f_data[ $fName ];
// Remove Form Field Name Without Keywords.
unset( $this->_f_data[ $fName ] );
}
?>
|
In Part 1, we explained that all form data sent by PHP is
either saved in a $POST or $GET Super Global. To make life easier for us, we
save this data in a Class Attribute depending on the form method so we do
not always have to check both Super Global's.
We want to add some sort of error check to our Class, to avoid adding non
existent fields. This is to ensure that our Class will run at the fastest
possible speeds. If non existent fields are allowed to be added, not only
will PHP parser warning be generated, but our Class will loop through an
endless number of form fields that simply do not exist.
<?php
if( count( $this->_f_data ) == 0 )
{
$this->_l_sError[] = 'addFormField() Method : No Form Data : Ignore Add Field : ' .
'<strong>' . $fName . '</strong>';
return;
}
?>
|
In the above code segment, we make sure the form was submitted, there hence
making sure that there is some sort of form data to work with. Technically we
can skip the top step, since the next code segment also does some sort of error
check, but leaving it won't hurt.
<?php
if( array_key_exists( $fName , $this->_f_data ) == FALSE )
{
$this->_l_sError[] = 'addFormField() Method : Non Existant Form Field : ' .
'<strong>' . $fName . '</strong>';
return;
}
?>
|
Again here we make use of the PHP function array_key_exists( ). If you remember
from Part 1, we said that the array housing our form data has as keys each form
field name.
So by checking that there is a key with our form field name that we wish to add
does indeed exist in our array, we make sure that we are not adding a non
existent field. The above code segment returns a Class system error message if
the key does not exist.
Having made sure that no errors will be encountered when adding a new field, we
then proceed to add a new field to our array, with the keywords included. This
is a two step process.
First we add to our array a new key with a value of the keywords followed by the
field name. Then we remove the old key to avoid any errors.
What does this mean? Well, let's say in our array we had a key of Email and we
wanted to add a keyword of (Required). We will create a new key in our array
called (Required)Email then remove the old key of Email alone.
This is exactly what we have done in the code below.
<?php
// Add Keywords.
$this->_f_data[ $keyword . $fName ] = $this->_f_data[ $fName ];
// Remove Form Field Name Without Keywords.
unset( $this->_f_data[ $fName ] );
?>
|
- Tutorial written by Limitless
|

|
|
 |
[WIP] Hairdresser Site Design Author: YinToniq Posted: Jul 03rd, 5:58am Activity: 1 replies, 38 views
|  | Simple Flash Help Needed (AS2) Author: stiney51 Posted: Jul 02nd, 2:40pm Activity: 9 replies, 56 views
|  | Transformers Author: Jacorre Posted: Jun 29th, 6:33pm Activity: 2 replies, 75 views
|  | Quick question about Jasc PaintShop Pro 9. Author: Rydium-41 Posted: Jun 28th, 8:21pm Activity: 2 replies, 73 views
|  | I bought film today!! Author: NikonErik Posted: Jun 27th, 12:04am Activity: 2 replies, 75 views
|  | New Battle Author: MoodsR4Cattle Posted: Jun 26th, 6:28pm Activity: 2 replies, 84 views
|  | Image Loading Author: funkyfela Posted: Jun 22nd, 9:00pm Activity: 1 replies, 85 views
|  | Headshot Critique Please Author: NikonErik Posted: Jun 19th, 11:32pm Activity: 6 replies, 153 views
|  | imagecopy() and imagecopyresized() problem Author: thatpyrokid Posted: Jun 18th, 3:05pm Activity: 1 replies, 151 views
|  | Problem With Regitration Code...? Author: RXS Posted: Jun 17th, 3:18am Activity: 2 replies, 150 views
|  | Topaz Labs do it again! Author: Tamlin Posted: Jun 17th, 12:08am Activity: 5 replies, 229 views
|  | [Photoshop] Anime Coloring Tutorial Author: Wavechan Posted: Jun 16th, 3:47pm Activity: 2 replies, 292 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|