Home arrow PHP arrow Page 3 - Building a PHP 5 Form Processor: Using the Form Processor Package

Performing server-side validation: implementing the “validator” module - PHP

Welcome to the last part of the series “Building a PHP 5 form processor.” In three parts, this series goes through the development of an extensible form processor package in PHP 5, explaining its benefits and demonstrating its implementation.

TABLE OF CONTENTS:
  1. Building a PHP 5 Form Processor: Using the Form Processor Package
  2. Getting started: constructing programmatically an online form
  3. Performing server-side validation: implementing the “validator” module
  4. Gluing the pieces: putting the form processing modules to work together
  5. The complete application at a glance: listing the complete code of the form processor
By: Alejandro Gervasio
Rating: starstarstarstarstar / 22
January 30, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: