As you know, in addition to basic client-side validation performed before submitting the form, the “validator” module, which I developed in the previous article, exposes some handy methods for verifying on the server whether user-supplied data is valid or not. It’s precisely for this reason that I’ll write a little piece of code, which validates the data entered in the required fields. The checking script looks like this: if($_POST['send']){ // instantiate form validator object $fv=new formValidator(); $fv->validateEmpty('fname','Enter your first name (Min: 4 characters)'); $fv->validateEmpty('lname','Enter your last name (Min: 4 characters)'); $fv->validateEmail('email','Enter a valid email address'); // check for errors if($fv->checkErrors()){ // display errors echo '<h2>Resubmit the form after correcting the following errors:</h2>'; echo $fv->displayErrors(); } else { echo '<h2>Thank you for submitting your data!</h2>'; } } If you study the above script, you’ll see that after submitting the form, a form “validator” object is instantiated, in order to verify the data of each required field. In this example I’ve used the “validateEmpty()” method for checking whether the values entered for “First Name” and “Last Name” are empty strings or not. Similarly, the “email” field is checked by the “validateEmail()” method. After running the server-side checking script and assuming that invalid data was submitted by the form, the output might look like this:
As you can see, the “formValidator” class does a decent job validating the required fields. Of course, the look and feel for displaying error messages should be styled properly, but I guess you have a pretty clear idea of how the server-side validation process works. Having demonstrated the nice capabilities of both form processing modules, it’s time to show how they fit into a single script. Considering this situation, jump into the next section to learn how these modules are implemented on the same PHP file.
blog comments powered by Disqus |
|
|
|
|
|
|
|