#!/usr/bin/perl # an example of form validation # by Mark Stosberg # licensed under the GPL, details at http://www.gnu.org/ # # 2.01 06/22/03, update to use check_rm (William McKee) # # 2.0, created on 04/20/03 to use CGI::Application and CGI::Application::ValidateRM # example-form-validation.cgi contains simply this: # # use ExampleFormValidation; # my $webapp = ExampleFormValidation->new(); # $webapp->run(); package ExampleFormValidation; use base CGI::Application; use CGI::Application::Plugin::ValidateRM (qw/check_rm/); use strict; sub setup { my $self = shift; $self->start_mode('form_display'); $self->run_modes([qw/ form_display form_process /]); } sub form_display { my $self = shift; my $errs = shift; my $t = $self->load_tmpl('example.tmpl',die_on_bad_params=>0); $t->param($errs) if ref $errs; return $t->output; } sub form_process { my $self = shift; my ($results,$err_page) = $self->check_rm('form_display', { required => [qw/full_name/], optional => [qw/ pet_name pet_food zipcode fav_color fav_music fav_language /], dependencies => { pet_food => [ 'pet_name'], }, constraints => { zipcode => 'zip', fav_language => '/Perl/i', }, # trim leading and trailing whitespace from all the valid fields. filters => ['trim'], msgs=>{ any_errors => 'err__', prefix=>'err_', }, }); return $err_page if $err_page; # Usually something is done with $results->valid here. $self->header_type('redirect'); $self->header_props( -url=>'index.html'); } 1;