How To Add More Validation Methods
We just simply provided a very simple example. The idea in adding more validation Methods is pretty simple. All you need when coding new validation Methods is an idea on how they work.
First of, all validation Methods must in the form of:
_vm_nameOfValidationMethod
Where nameOfValidationMethod above will also correspond to the keyword you will add to your form fields.
Second, all validation Methods must accept two arguments in the form of:
_vm_nameOfValidationMethod( $data , $pos )
You do not have to worry about how to pass values to these arguments. They are done for you automatically. One thing
you do have to worry about, however, is how to actually use them. From the example before, you can pretty much
get an idea but just in case here is a rerun...
You should only use the variable $pos when you want to print the form field's name. You use it as the
key in the $_f_tName array like so:
Example 3: Retrieve Form Field Name From Array
<?php
$this->_f_tFName[ $pos ];
?>
$data is the actual data that is entered in the form field. You can do whatever check you want on it. For example,
you can compare it to a value, see if it is bigger or smaller than a number, or if its a specific length or not, etc.
Any validation errors (that is error messages you want to print for the user because validation failed for whatever reason), you should
add to the $_l_fError array, like we did in the previous example:
Example 4: Adding Error Messages
<?php
this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] . '</strong>' . ' is Required.';
?>
If you remember in Part 2, we created a Method to easily return an array of error messages for ALL form
fields so that we can display them however we like.
If all of this is still not clear enough, do not worry. It will become more clear when more validation Methods are
presented at the end.
Using The Class
Now that we have a test validation Method and we have explained how to expand on the Class, let's show a visual example
on how the Class can be used in a actual situation. For this example, we will assume the Class source is saved
in an external file which we will
include( )
in our test script.
Let's show some code first than explain each section one at a time.
Example 5: How To Use The Class
<?php
// Load Required Libarys.
require_once 'path_to_class_src.php';
/**
* Create Operation Object.
*
* This is the object for our Class that we wil use in our script. The
* Consturctor accepts one argument, which is the form submission method.
* Use 'p' for POST or 'g' for GET.
*/
$vObject = new datVForm( 'p' );
/**
* Validate Form.
*
* The form, if submitted, will be validated. Before the form can be
* validated, the Class must know which form fields to validate
* and their keywords.
*/
/**
* Check If Form Was Submitted.
*
* For a POST form submission Method, all data in the form
* is saved in PHP's $_POST Super Global array. Thus a check
* to make sure it contains any elements will tell us
* if the form was submitted or not.
*/
if( count( $_POST ) > 0 )
{
/**
* Retrive Form Fields.
*
* We save each form field into its own variable
* so we can refrence it easier later in our
* script. Also each form field is trimmed
* to make sure trailing white space
* characters are removed.
*
* Remember that each variable is an actual
* form field, whose name is dertemined
* by the form fields 'name' attribute.
*/
$_POST[ 'Real_Name' ] = trim( $_POST[ 'Real_Name' ] );
$_POST[ 'Email_Address' ] = trim( $_POST[ 'Email_Address' ] );
/**
* Define Validation Rules.
*
* Each form field is validated differently. We define
* how each form field should be validated to the
* Class.
*
* The first argument is the name of our form field
* and the second argument is the keywords. More
* than one keyword is possible but for this example
* we only have one keyword.
*/
$vObject->addFormField( 'Real_Name' , '(Required)' );
$vObject->addFormField( 'Email_Address' , '(Required)' );
/**
* Validate.
*
* We save the BOOL result in a variable. Remember that
* validate( ) returns either TRUE or FALSE based on
* if each form field passed its validation rule.
*/
$fStatus = $vObject->validate( );
}
// Check If Form Was Validated Correctly.
// Errors Encountered.
if( isset( $fStatus ) && ( $fStatus == FALSE ) )
{
echo 'strong>The Following Errors Exist :</strong><br /><br />';
// Get Form Error Messages.
$errorMessages = $vObject->getFormError( );
// Loop And Print Each Error In A List.
// Remember that each element in the
// array is an error message.
echo '<ul>';
for( $i = 0; $i < count( $errorMessages ); $i++ ) {
echo '<li>' . $errorMessages[ $i ] . '</li>';
}
echo '</ul>';
}
else {
// ... Validation Passed. Do Something With Our Script ...
}
// Below Is The Actual HTML for our form. One thing to notice
// is that 'name' attribute for each form field.
?>
<form action="path_to_this_script" id="Test_Form" method="post">
<input name="Real_Name" maxlength="30" size="30" type="text" />
<input name="Email_Address" maxlength="30" size="30" type="text" />
<input name="Button" type="submit" value="Click Me" />
</form>
That is basically how you would use the Class. The comments explain each section but there is one thing to note.
The getFormError( ) Method returns an array of validation errors. I did nothing to format the display of the error messages,
I just printed them into a list. You can pretty much print the error messages anywhere in the HTML you want, even in the HTML code itself.
More Validation Methods
All the examples before assumed that we only had one validation Method to work with. Chances are, there are a limitless number of
ways you may want to validate your form data and to make things easier for you, so we will provide some of them now.
All you have to do is add them to your Class body.
To use them is no different than we provided in Example 5 above. The only difference is the section where we instruct our Class on how
to validate each form field, to which we would add the appropriate keywords:
Example 6: Adding Multiple Keywords To Form Fields
<?php
$vObject->addFormField( 'Real_Name' , '(Required)(Keyword2)(Keyword3)' );
?>
Okay, let's show some more useful validation Methods you can add to your new Class:
Example 7: Positive Number Validation Method
<?php
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.';
}
}
?>
Example 8: Password Validation Method
<?php
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 );
}
?>
Example 9: Numeric Characters Only Validation Method
<?php
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.';
}
}
?>
Example 10: Alpha-Numeric Characters Only Validation Method
<?php
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.';
}
}
?>
- Tutorial written by Limitless
|