HomePHP Building a PHP 5 Form Processor: Coding the Form Generator Module
Building a PHP 5 Form Processor: Coding the Form Generator Module
Most developers must deal with the job of developing robust data validation for forms on a regular basis. Nobody wants to write the same code over and over, so this article (the first of three in a series) will help you develop an extensible and reusable form validation package. By the end of the article, you will have all the relevant classes that integrate the generator module of the PHP form processor.
As you’ll probably agree, one of the most common tasks during the development of a web application consists of writing checking code for validating user input, submitted by online forms. From a developer’s point of view, this might be sometimes an annoying activity, because it’s one of the things that you’re likely to do over and over again. Moreover, if you write and release an application which lacks form user data validation routines, sooner or later it will be broken up, due to the constant submission of erroneous data, either because of ignorance or maliciousness.
Considering that developing robust data validation is something that doesn’t bear discussion, and certainly is one of those jobs that most developers have to face very often, the best way to tackle the problem is either by picking up an existing validation library, or building in a reusable package. If you’re feeling inclined toward the first option, you can start using one of the numerous form checking libraries available on the Web. It’s just a few clicks away.
However, for those developers wanting to dive into the development of an extensible and reusable form validation package, this article might serve as an introductory process for developing and implementing an object-based form generation-verification solution. Since PHP supports creating classes in a very friendly way, in this first article I’ll walk through a step-by-step format to build some highly generic, customizable PHP 5 form generation classes. This first library of classes will comprise what I call a “generator” module, and allow you to programmatically construct (X)HTML forms, offering basic client-side validation features. Similarly, the second tutorial of this series will focus on developing a “validator” module, which will include a useful server-side validation class.
The end result of this experience will be a PHP 5 form processor, which can be customized with little difficulty, so it can work for you. Before I start coding the application’s first module, I’d like to state a few requirements: you’ll need to have an intermediate knowledge of object programming in PHP 5, as well as a basic familiarity with some of the PHP built-in data introspection functions.
With the formalities out of the way, let’s get started.