|
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 a long awaited conclusion to this highly received tutorial series, we help wrap up our Form Validation Class.
It is highly recommended that you have read, completed, and fully understood Part 1 and Part 2
of this tutorial series to fully understand what is going on here in this last Part.
Since we will basically be summarizing what we concluded in those Parts and building upon them here, if you have not read them
you will pretty much be in the dark.
In this final Part we will be coding some example validation rules and explaining how to use them. We will then go on and
show how the final complete Class can be applied in a real working environment, so that you can use it in all your future scripts.
At the end of this Part, you should have gained a complete understanding of how the Class operates, and proceed to
expand on it with ease.
A Quick Summary
Here is a quick summary of what we did in Part 1 and Part 2. At the end Part 2, you will remember that
the Class now contains all the major elements to get it to work, which are:
- Validation Method.
- Error Messages.
- Form Fields To Be Validated.
Like I pointed out, the Validation Method is in charge of finding the keywords associated with each form field that instruct the Class how to validate it.
Adding Form Fields is just a way of telling the Class which form fields to validate.
There is one thing missing which I will cover in this Part. And that is the actual validation rules.
If you remember, each keyword is like an actual rule. The rules are in the form of Class Methods, where the name of each Method is the name of each
keyword assigned with _vm added to the start.
To help our Class know what is a keyword and what is the name of each field, the keywords are enclosed in parenthesis, ( ).
eg. a keyword of (Required) will call a Method called _wm_required( ), where the body
of each Method will contain the actual code to validate.
A Simple Validation Example
Let's provide an example on how to create a simple validation rule in the form of a Method to test our Class.
In most web forms, some fields will be required for the user to enter, that is they can not be left blank. This is very simple
to test for. In PHP, we can simply grab the value entered in the form field. If it is empty or NULL string, than nothing was
entered. Therefore an error will be triggered.
Add the following Method to your Class body, which should already contain all the previous code from Part 1 and Part 2. If you get confused, do not worry. At the end of this Part, we will
provide a complete code for everything covered in all 3 Parts.
Example 1: Require Validation Method
<?php
function _vm_required( $data , $pos )
{
// Determine If Value Is Set.
if( $data == NULL ) {
$this->_l_fError[] = '<strong>' . $this->_f_tFName[ $pos ] . '</strong>' . ' is Required.';
}
}
?>
This is a validation Method that will be called when a keyword of (Required) is assigned to a form field.
What it does is simply check if there is a value set for the form field. If there is, then the field is not empty, thus
it passes the rule. The check is done with the IF Statement.
The most important thing to note, however, are the two arguments the Method accepts. It is quite important to understand that each
validation Method you create has these two arguments. These arguments are passed automatically by the validate( ) Method in Part 1 of
the tutorial.
$data is the actual form field value entered by the user. $pos is the position of the form field in the form.
How are they used? They are basically a convenience:
$data is used to check the value to validate. Each Validation Method is unique in the way it checks the data passed to it.
In this example's case, we check that $data is not NULL because if it is, that means no value for it was entered, which is what we want to check for.
$pos is used to retrieve the form field's name that is being validated from the array in order to generate our error
message if the $data is indeed NULL.
We save our error messages in a Class Attribute of type array in order to retrieve it and display it later in any way you want.
The form field names are saved in an array called _f_tFName, so by doing this code:
Example 2: Retrieve Form Field Name From Array
<?php
$this->_f_tFName[ $pos ];
?>
We have basically retrieved the form field's name. Again refer to the validate( ) Method.
Now we have something to work with. To use this Method, obviously to check if a required form fields were
entered or not, all we have to do is add the keyword (Required) to each form field
we want to check. The Class will take care of the rest! Pretty simple.
- Tutorial written by Limitless
|