Summersault
Home About Us Services Portfolio Community Support
Database Driven Websites
community home
local community
partner community
online community
blog


Archives: Categories: Authors:

 

Summersault Blog

Validating Web forms with Perl

Posted by Mark Stosberg on October 25th, 2005

One of the most frequent uses of Perl in my job is to validate web-based forms. Here’s some explanation and sample code to how I do that efficiently and effectively.


screenshot of validated form

As a foundation, I use the CGI::Application framework. I originally thought using a “framework” for simply displaying and validating a form as overkill. I now realize it’s not.

First, you’ll notice how concise the code is. There is very little overhead with CGI::Application. CGI::Application also helps with re-usability by moving all code into modules. This maximizes code re-us across projects, saving time in the long run.

The heavy lifting for the form validation will be done by Data::FormValidator. The example I’ve given here is faily simple. Data::FormValidator helps with validating much more complicated cases as well.

This solution may require installing these modules on your server, as well as CGI::Application::Plugin::ValidateRM and the dependencies of these modules.

If these modules aren’t already installed on your system, and you don’t have root access, you can still install and use them in your own directory by including something like this in the installation process:

perl Makefile.PL LIB=/home/user/perllib \
        INSTALLMAN1DIR=/home/user/man/man1 \
        INSTALLMAN3DIR=/home/user/man/man3 \
        INSTALLBIN=/home/user/bin \
        INSTALLSCRIPT=/home/user/scripts

Of course, you’ll need to include a line like use lib ('/home/user/perllib'); once you’ve done this.

Setting up the templates

To get this tour started, let’s first set up a template with a form to validate. To make it interesting, we’ll have a required field, some optional fields, a field that is required if another one is filled in, one with a generic validation constraint, and one with a custom validation constraint.You can view the source of example.tmpl, or go ahead and try out the demo. Some browsers may render example.tmpl as HTML. You can also right-click it and select “Save target as…”

Writing the Perl code

The next step is to write the code that will display the form and validate it’s contents. This is probably better explained by direct example than description, so you can view the code I use:

For a long time I did form validation without these extra modules, and I find that this system is much faster, and produces a better user interface by showing the errors on the same page where the user can correct them.

If you have ideas about how this system could be further improved, I’m interested to hear them. Thanks and good luck!

Further resources

You may also be interested in the Data::FormValidator::Tutorial.


Did you find this entry interesting or useful? Please tell us about it!

3 Responses to “Validating Web forms with Perl”

  1. James Says:

    An example using Template Toolkit would be nice. :-)

  2. frank Says:

    Great example using cgi-app. I’m just a beginner with cgi-app and this helped a lot. Thanks Mark

  3. geetha Says:

    Excellent example. This is very useful. Thanks

Leave a Reply

The opinions expressed by individuals posting in the Summersault Blog are not necessarily those of Summersault, LLC. While we try to insure the quality and accuracy of the information presented here, we make no guarantees about its suitability for any particular purpose.