Building An Extensible Form Validator Class (
Page 1 of 12 )
Wondering what OOP can do for you? Well, wonder no more - this
article demonstrates how OOP can save you time and effort by building a
PHP-based Form Validator object to validate HTML form input. In addition
to a detailed walkthrough of the process of constructing a PHP class to
test user input, this article also includes usage examples and a brief
look at some powerful open-source alternatives.One of my most common activities during the development cycle for a Web
application involves writing code to validate the data entered into online
forms.
This might seem like a trivial task, but the reality - as anyone
who's ever spent time developing a robust Web application will tell you - is
completely different. Data verification is one of the most important safeguards
a developer can build into an application that relies on user input, and a
failure to build in this basic error checking can snowball into serious problems
(and even cause your application to break) if the data entered is corrupt or
invalid.
In order to illustrate this, consider a simple example, culled
from my own experience: an online loan calculator that allows a user to enter
the desired loan amount, finance term and interest rate. Now, let's assume that
the application doesn't include any error checks. And let's also suppose that
the user decides to enter that magic number, 0, into the term field.
I'm
sure you can imagine the result. The application will perform a few internal
calculations that will end in it attempting to divide the total amount payable
by the specified term - in other words, division by zero. The slew of ugly error
messages that follow don't really bear discussion, but it's worth noting that
they could have been avoided had the developer had the foresight to include an
input-validation routine while designing the application.
The things
about data validation, though, is that it's one of those things you can't hide
from. Even if you develop and release a Web application without building in any
validation routines (either through ignorance or laziness), you can be sure that
your customer's going to demand a fix for it in the next release of the
software. And since - as I said right at the beginning of this article - it's
one of the few things you're likely to do over and over again when building Web
applications, it's worthwhile spending a little time to make the process as
painless as possible.
That's where this article comes in. Over the next
few pages, I'll be attempting to build a reusable library of functions for form
input validation, in an attempt to save myself (and, hopefully, you) some time
the next time an application needs to have its input checked for errors. The end
result of this experiment will be a PHP class that can be easily included in
your scripts, and that exposes basic object methods for data validation. It may
not meet *all* your needs; however, the process should be instructive,
especially if you're new to object programming in PHP, and you'll have very
little difficulty customizing it so that it works for you.
Let's get
going!