<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

#-----------------------------------------------#
# Use this program to get a feel for
# how to use WWW::Form and WWW::FieldValidator
#
# This program must be placed in a web 
# accessible and CGI executable location
# in order for it to run properly
#-----------------------------------------------#

use strict;
use warnings;

use CGI;
use Data::Dumper;

# both of these need to be installed to run this
# test program
use WWW::Form;
use WWW::FieldValidator;

# gets us access to the HTTP request data
my $q = CGI-&gt;new();

# hash ref of HTTP vars
my $params = $q-&gt;Vars();

# this gets us our Form object
my $form = getForm();

# display the HTML form test page
printHTMLPage();

#-----------------------------------#
# Start subroutines needed to build
# Form test page
#-----------------------------------#

sub printHTMLPage {

print &lt;&lt;HTML;
Content-Type: text/html

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Form Test Page&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
HTML

    print "&lt;p&gt;WWW::Form version: $WWW::Form::VERSION&lt;br /&gt;";
    print "WWW::FieldValidator version: $WWW::FieldValidator::VERSION&lt;/p&gt;";

    print "HTTP POST Variables\n&lt;pre&gt;" . Data::Dumper::Dumper($params) . "&lt;/pre&gt;";

    # uncomment the following Data::Dummper line if you 
    # want to look at the internal structure of the Form module
    #print "Form object\n&lt;pre&gt;" . Data::Dumper::Dumper($form) . "&lt;/pre&gt;";

    print "\n&lt;h2&gt;" . getFormStatusMessage() . "&lt;/h2&gt;\n";

    print "&lt;form action='./form_test.pl' method='post'&gt;\n";
    print "&lt;table border='0' cellspacing='2' cellpadding='5'&gt;\n";
    print $form-&gt;get_field_HTML_row('name');
    print $form-&gt;get_field_HTML_row('emailAddress');
    print $form-&gt;get_field_HTML_row('password', ' size="6" ');
    print $form-&gt;get_field_HTML_row('passwordConfirm', ' size="6" ');
    print $form-&gt;get_field_HTML_row('comments', " rows='5' cols='40' ");
    print $form-&gt;get_field_HTML_row('favoriteColor');
    print $form-&gt;get_field_HTML_row('elvisOrBeatles');
    print $form-&gt;get_field_HTML_row('spam');
    print $form-&gt;get_field_HTML_row('aHiddenInput');
    print "&lt;/table&gt;\n\n";

print &lt;&lt;HTML;
&lt;input type="submit" value="Submit" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
HTML
}

# uses the isSubmitted, validateFields, and isValid methods
# of WWW::Form object
sub getFormStatusMessage() {
    # init status message to display
    # in the form test web page
    my $formStatusMessage = 'Form has not been submitted';

    # check to see that the form was submitted
    if ($form-&gt;isSubmitted($ENV{REQUEST_METHOD})) {

        # the form was POSTed so
        # validate the user entered input
	$form-&gt;validateFields($params);

        # update our status message depending on
	# whether or not the form data was good
        # if the form data is good then do some stuff
	if ($form-&gt;isValid()) {
	    $formStatusMessage = 'Form was submitted and the data is good';
	} else {
	    $formStatusMessage = 'Form was submitted and the data is bad';
	}
    }
    return $formStatusMessage;
}

# creates and return a WWW::Form object
sub getForm {
    # if there are HTTP params then use those
    # values to instantiate our Form object with
    if ($params) {
        return WWW::Form-&gt;new(getFormFields(), $params);
    } else {
        return WWW::Form-&gt;new(getFormFields());
    }
}

# returns data structure suitable for passing
# to Form object constructor
# this example covers how to handle all of the various
# types of form inputs with WWW::Form
sub getFormFields {
    my %fields = (
        emailAddress =&gt; {
            label        =&gt; 'Email address',
            defaultValue =&gt; '',
	    type         =&gt; 'text',
            validators   =&gt; [WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::WELL_FORMED_EMAIL,
                                'Make sure email address is well formed')]
        },
        name =&gt; {
            label        =&gt; 'Full name',
	    defaultValue =&gt; '',
	    type         =&gt; 'text',
            validators   =&gt; [WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::MIN_STR_LENGTH,
                                'Please enter your name (at least 3 characters)', 3)]
	},
        aHiddenInput =&gt; {
            label        =&gt; '',
	    defaultValue =&gt; 'Hey, I am a hidden form input, nice to meet you!',
	    type         =&gt; 'hidden',
            validators   =&gt; []
	},
        password =&gt; {
            label        =&gt; 'Password',
	    defaultValue =&gt; '',
	    type         =&gt; 'password',
            validators   =&gt; [WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::MIN_STR_LENGTH,
                                'Password must be at least 6 characters', 6)]
	},
        passwordConfirm =&gt; {
            label        =&gt; 'Confirm password',
	    defaultValue =&gt; '',
	    type         =&gt; 'password',
            validators   =&gt; [WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::MIN_STR_LENGTH,
                                'Password confirm must be at least 6 characters', 6),
			     WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::REGEX_MATCH,
                                'Passwords must match', '^' . $params-&gt;{password} . '$')]
	},
        spam =&gt; {
            label          =&gt; 'Do we have your permission to send you spam?',
	    defaultValue   =&gt; 'Yes, spam me.',
	    defaultChecked =&gt; 0, # set to 1 to check by default
	    type           =&gt; 'checkbox',
            validators     =&gt; []
	},
        comments =&gt; {
            label        =&gt; 'Comments',
	    defaultValue =&gt; '',
	    type         =&gt; 'textarea',
            validators   =&gt; [WWW::FieldValidator-&gt;new(
                                WWW::FieldValidator::MIN_STR_LENGTH,
                                "If you're going to say something, how about at least 10 characters?",
				10,
				my $isOptional = 1)]
	},
        favoriteColor =&gt; {
            label        =&gt; 'Favorite color',
	    defaultValue =&gt; '', # set to 'green', 'red', or 'blue' to set default option group
	    type         =&gt; 'select',
            optionsGroup =&gt; [{label =&gt; 'Green', value =&gt; 'green'},
			     {label =&gt; 'Red',   value =&gt; 'red'},
			     {label =&gt; 'Blue',  value =&gt; 'blue'}],
            validators   =&gt; []
	},
        elvisOrBeatles =&gt; {
            label        =&gt; 'Do you like Elvis or the Beatles',
	    defaultValue =&gt; 'I am a Beatles dude(tte)', # uncomment to leave group unchecked by default
	    type         =&gt; 'radio',
            optionsGroup =&gt; [{label =&gt; 'I like Elvis',       value =&gt; "I am an Elvis dude(tte)"},
			     {label =&gt; 'I like the Beatles', value =&gt; "I am a Beatles dude(tte)"}],
            validators   =&gt; []
	}
    );
    return \%fields;
}
</pre></body></html>