| Expandable Form Validation Class : Part 3 |
pages (3): 1 2 [3] |
|
The Complete Final Class
Below is the complete final Class, including all code from Part 1, Part 2, and this Part 3 for your reference. You can easily
expand on the Class using your new knowledge.
I hope you enjoyed this lengthy tutorial series and learned something new to add to your coding knowledge.
Example 11: The Complete Class
<?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;
////////////////////////////////////////////////////////////
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>';
}
}
////////////////////////////////////////////////////////////
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.
$temp = substr( $this->_f_cFName[ $i ] , $l_nameStart + 1 );
$this->_f_tFName[] = str_replace( '_' , ' ' , $temp );
}
}
////////////////////////////////////////////////////////////
function _vm_alnum( $data , $pos )
{
// Ensure Data Is Not NULL.
if( $data == NULL )
{
$this->_l_sError[] = '_vm_alnum() Method : NULL Data : Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $pos ] .
'</strong>';
return;
}
if( ctype_alnum( $data ) == FALSE ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] . '</strong>' .
' must contain Alpha-Numeric characters only.';
}
}
////////////////////////////////////////////////////////////
function _vm_numeric( $data , $pos )
{
// Ensure Data Is Not NULL.
if( $data == NULL )
{
$this->_l_sError[] = '_vm_numeric() Method : NULL Data : Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $pos ] .
'</strong>';
return;
}
// Determine If Numeric Characters Exist Only.
if( is_numeric( $data ) == FALSE ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] .
'</strong>' . ' must contain Numeric characters only.';
}
}
////////////////////////////////////////////////////////////
function _vm_pass( $data , $pos )
{
// Ensure Data Is Not NULL.
if( $data == NULL )
{
$this->_l_sError[] = '_vm_pass() Method : NULL Data : Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $pos ] .
'</strong>';
return;
}
// Determine If At Least 8 Characters Exist.
if( strlen( $data ) < 8 ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] .
'</strong>' . ' must contain at least 8 characters.';
}
// Determine If Alpha-Numeric Characters Exist Only.
$this->_vm_alnum( $data , $pos );
}
////////////////////////////////////////////////////////////
function _vm_pos( $data , $pos )
{
// Ensure Data Is Not NULL.
if( $data == NULL )
{
$this->_l_sError[] = '_vm_pos() Method : NULL Data : Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $pos ] .
'</strong>';
return;
}
// Ensure Numeric Characters Are Passed.
if( is_numeric( $data ) == FALSE )
{
$this->_l_sError[] = '_vm_pos() Method : Non Numeric Data : Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $pos ] .
'</strong>';
return;
}
// Determine If Postive Number And Not Zero.
if( $data <= 0 ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] . '</strong>' .
' must contain a positive number bigger than zero.';
}
}
////////////////////////////////////////////////////////////
function _vm_required( $data , $pos )
{
// Determine If Value Is Set.
if( $data == NULL ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] .
'</strong>' . ' is Required.';
}
}
////////////////////////////////////////////////////////////
function addFormError( $error ) {
$this->_l_fError[] = $error;
}
////////////////////////////////////////////////////////////
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 : ' .
'Ignore Validation : ' .
'<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 ] );
}
////////////////////////////////////////////////////////////
function getFormError( ) {
return $this->_l_fError;
}
////////////////////////////////////////////////////////////
function getSystemError( ) {
return $this->_l_sError;
}
////////////////////////////////////////////////////////////
function validate( )
{
// Define Local Variables.
$l_dataCount = count( $this->_f_data );
$l_key = array( );
$l_keyCount = NULL;
$l_vm = NULL;
// Find Complete Field Names.
$this->_f_cFName = array_keys( $this->_f_data );
// Find Field Names Without Keywords.
$this->_s_tFName( );
// Loop.
for( $i = 0; $i < $l_dataCount; $i++ )
{
// Determine If Keyword Exists In Field Name.
preg_match_all( '/\((.*?)\)/s' , $this->_f_cFName[ $i ] , $l_key );
// Determine Number Of Keywords, If Any.
$l_keyCount = datLib::array_count_2D( $l_key , 0 );
// Loop.
for( $j = 0; $j < $l_keyCount; $j++ )
{
if( method_exists( $this , '_vm_' . strtolower( $l_key[ 1 ][ $j ] ) ) == TRUE )
{
// Extract Keyword Validation Method.
$l_vm = '_vm_' . strtolower( $l_key[ 1 ][ $j ] );
// Call Validation Method.
$this->$l_vm( $this->_f_data[ $this->_f_cFName[ $i ] ] , $i );
}
else {
$this->_l_sError[] = 'validate() Method : Non Existant Keyword : ' .
'Ignore Validation : ' .
'<strong>' . $this->_f_tFName[ $i ] . '</strong>';
}
}
}
// Return.
if( ( datLib::array_isNull( $this->_l_fError ) == FALSE ) ||
( datLib::array_isNull( $this->_l_sError ) == FALSE ) )
{
return FALSE;
}
/**
* Return TRUE.
*
* The Method will only return TRUE if there are no validation errros.
* Return TRUE means that every field has been validated
* correctly and it is now generally safe to use this form data.
*/
return TRUE;
}
////////////////////////////////////////////////////////////
}
?>
- Tutorial written by Limitless
| 
There are no comments for this tutorial yet. You can place a comment by clicking here.
|

|
|
 |
Help Author: kitty.tina Posted: Nov 19th, 7:48pm Activity: 2 replies, 36 views
|  | Zen Author: hackzenindus Posted: Nov 19th, 2:54pm Activity: 6 replies, 79 views
|  | Submit/sell/show my designs Author: lakotaswift@hot Posted: Nov 19th, 12:38pm Activity: 1 replies, 30 views
|  | Are the Tutorials available??? Author: LivingCovers Posted: Nov 18th, 10:15am Activity: 2 replies, 48 views
|  | Graphic Artist from Nigeria, Africa... Author: LivingCovers Posted: Nov 18th, 8:47am Activity: 3 replies, 43 views
|  | New guy in VA Author: Rohape Posted: Nov 18th, 1:53am Activity: 4 replies, 51 views
|  | Doseages and Solutions Author: Guardian Posted: Nov 17th, 11:54pm Activity: 7 replies, 78 views
|  | Removing background from images Author: Alex Ross Posted: Nov 17th, 3:50pm Activity: 13 replies, 244 views
|  |
|
 |
 |
 |
 |
 |
| --- Site Resources --- |
| Total Tutorials: | 210 |
| Total Downloads: | 413 |
| Linkbase Links: | 243 |
 |
|
 |
 |
|