| Expandable Form Validation Class : Part 1 |
pages (4): 1 [2] 3 4 |
|
The Class Attributes
<?php
class datVForm
{
/**
* Form field names including the keywords.
*
* @var array
*/
var $_f_cFName;
/**
* Form data.
*
* @var array
*/
var $_f_data;
/**
* Form field names without the keywords.
*
* @var array
*/
var $_f_tFName;
/**
* Form method.
*
* @var string
*/
var $_f_type;
/**
* Form validation error messages.
*
* @var array
*/
var $_l_fError;
/**
* Class system error messages.
*
* @var array
*/
var $_l_sError;
//////////////////////////////////////////////////////////////////////
?>
|
The Class Attributes are the variables that are shared between all Class
Methods. From the comments you can pretty much determine what each Attribute is
used for, but there are several that need some clarification.
$_f_cFName is an array housing the form field names in the order in which
they appear in the form. The Class automatically appends each keyword associated
with each field to the start of it's name. This array houses each form field
name with the keywords included.
$_f_tFName is also an array housing the form field names in the order in
which they appear in the form. The only difference is that it does so without
the keywords included. Why? So when a form error is generated, only the field
name is included, not the keywords as well.
$_f_type is whether the form was submitted via a POST or GET method.
The Constructor
<?php
/**
* Constructor.
*
* The Constructor has several jobs. First based on the form
* method type, POST or GET, it will get all form data from
* the PHP Super Global. Also it defines the attributes that
* will be used by the Class.
*/
function datVForm( $f_type )
{
// Initialize Class Variables.
$this->_f_cFName = array( );
$this->_f_data = array( );
$this->_f_tFName = array( );
$this->_f_type = $f_type;
$this->_l_fError = array( );
$this->_l_sError = array( );
// Determine Form Method.
if( $this->_f_type == 'p' ) {
$this->_f_data = datLib::string_trim_array( $_POST );
}
else if( $this->_f_type == 'g' ) {
$this->_f_data = datLib::string_trim_array( $_GET );
}
else {
$this->_l_sError[] = 'datVForm() Method : Invalid Form ' .
'Method :: Ignore Validation : <em>' .
'All Fields</em>';
}
}
//////////////////////////////////////////////////////////////////////
?>
|
The Constructor Method is used when an object is first created. First, it
accepts one argument, which is the form submission Method. It uses it to save
the form data from either the POST or GET PHP Super Global. There are several
things to notice, particularly in this code section:
<?php
// Determine Form Method.
if( $this->_f_type == 'p' ) {
$this->_f_data = datLib::string_trim_array( $_POST );
}
else if( $this->_f_type == 'g' ) {
$this->_f_data = datLib::string_trim_array( $_GET );
}
else {
$this->_l_sError[] = 'datVForm() Method : Invalid Form ' .
'Method :: Ignore Validation : <em>' .
'All Fields</em>';
}
//////////////////////////////////////////////////////////////////////
?>
|
We used datLib Method string_trim_array to trim all
the form data before saving it in our Class. We did that so that no
extra white space characters are included to avoid any errors in
validation.
Consider a validation for a field that can not have any new line
characters in it for example. If the User by mistake included a new line
character by pressing the enter key before submitting the form, an error
message will always be triggered. To save us and the User the headache,
we just trim the data.
We also make sure that the form submission method was either POST or
GET, or a system error will be triggered to inform us that an invalid
method was detected.
- Tutorial written by Limitless
|

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