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

|
|
 |
php, shoutbox problems Author: vanhansen Posted: Nov 17th, 1:30am Activity: 5 replies, 92 views
|  | MarkupGeeks Logo Author: ahstanford Posted: Nov 16th, 8:45pm Activity: 11 replies, 149 views
|  | Drawing Tutorials Author: ahstanford Posted: Nov 16th, 12:46am Activity: 0 replies, 98 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, 96 views
|  | Learning to draw... Author: ahstanford Posted: Nov 15th, 12:43pm Activity: 4 replies, 115 views
|  | Looking for simple UI elements Author: FenixRoA Posted: Nov 15th, 6:40am Activity: 7 replies, 108 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, 140 views
|  | 4-man Simon Tournament Author: ahstanford Posted: Nov 11th, 3:28pm Activity: 0 replies, 90 views
|  | Design Brief Inspiration for BioRUST Battles! Author: ahstanford Posted: Nov 11th, 7:19am Activity: 4 replies, 139 views
|  | The BioRUST Free Stock Photography Thread Author: ahstanford Posted: Nov 11th, 6:32am Activity: 2 replies, 146 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 212 |
| Total Downloads: | 415 |
| Linkbase Links: | |
 |
|
 |
 |
|