A good form validation is done both on the client side and on the server side. Client side means that the validation is done in the browser using javascript. The advantage is that the form is not submitted, the page is not refreshed and the response is instant. The disadvantage is that you cannot perform complex validations or your client can simply turn off the javascript and all the magic is gone so it requires a backup plan on the server.
Since the tutorial is about PHP validation we're going to focus on the server side validation and learn how to build and validate user friendly web forms in PHP.
First, we need to make a list with things that we hate most when filling out forms:
- general errors ("Your information is not valid, please try again")
- the form does not remember my input after submitting it and I have to re-fill
- I don't know what's required or not
- all errors in a sigle place, usually at the top, causing me to ping-pong my eyes especially if I have a long form to fill
- errors like the above general one but also telling me to go back and try again
Now that we now what stresses people most, we need to address each and every one of them to make sure we're not doing these mistakes in our applications. Many programmers simply validate each field at each form in part the standard way but we're going to be smarter and make use of re-usable code. We're going to build a PHP class which can be instanciated in any place. Our scenario is actually built using 2 classes, one for the validation wich does the logic and the other holds the validators which consists of static methods just to perform the field validation and return boolean values like TRUE for passed, FALSE for failed.
The validation class is also supposed to store errors for each field in part in order to display them next to the field and nowhere else. This way is easier for our visitors to address them since the error message sites right next to the input. It also has to be smart enough and remember the values already posted by our visitor to avoid forcing him to re-fill all the inputs until everything is as requested by our validation scenario.
So, the form is submitted, the information is sent via POST and the server receives it. At that point, we instanciate our validation class and add all the validations to each field in part to make sure that what we receive and pass to further actions is the kind of data we expect. That's the whole point of a form validation anyways. Forcing people to submit what we expect making sure we don't encounter any errors.
We added the validation rules and next, we need to perform the validation itself, if any of the fields fail the validation, we stop the execution and present the form once again along with the errors marked in a nice red color. Once everything is received as we want and processed (database, email etc), we redirect the visitor to a success page and hide the form as we don't need it any more. This is what our target should be, a fast, smart and straightforward process for our visitors.
require_once "validation.php";
// instanciate the validation class
$validation = new Validation;
// subject validation rules
$validation->addField ( 'subject', 'required', 'This field is required' );
$validation->addField ( 'subject', 'min_length[5]', 'Your subject must contain at least 5 characters' );
$validation->addField ( 'subject', 'max_length[255]', 'Your subject must not contain more than 255 characters' );
// name validation rules
$validation->addField ( 'name', 'required', 'This field is required' );
$validation->addField ( 'name', 'min_length[5]', 'Your name must contain at least 5 characters' );
$validation->addField ( 'name', 'max_length[255]', 'Your name must not contain more than 255 characters' );
// email validation rules
$validation->addField ( 'email', 'required', 'This field is required' );
$validation->addField ( 'email', 'validEmail', 'Please add a valid email' );
// message validation rules
$validation->addField ( 'message', 'required', 'This field is required' );
$validation->addField ( 'message', 'min_length[20]', 'Your message must contain at least 20 characters' );
$validation->addField ( 'message', 'max_length[2000]', 'Your message must not contain more than 2000 characters' );
// execute the validation
if ( $validation->execute () ) {
// success, send the email, save the data in the database etc.
header ( "Location: success.php" );
}
// if we're here, the validation failed
As you can see, we can add unlimited validations to one field in part, each of them with a custom error message. This allows us to communicate better with the end user by building a smart, user friendly PHP form. Thank you for reading this, don't forget to download the example located at the end of this tutorial.
Validator.zip Read more ...