# Form.pm - The Form Object for the HTMLObject. Provides Form display and validation. # Created by James A. Pattie, 02/25/2004. # Copyright (c) 2004 Xperience, Inc. http://www.pcxperience.com/ # All rights reserved. This program is free software; you can redistribute it # and/or modify it under the same terms as Perl itself. package HTMLObject::Form; use strict; use HTMLObject::ErrorBase; use HTMLObject::Normal; use HTMLObject::CGILib; use HTMLObject::Widgets; use Data::FormValidator; use vars qw($AUTOLOAD $VERSION @ISA @EXPORT); require Exporter; @ISA = qw(HTMLObject::ErrorBase HTMLObject::Normal Exporter AutoLoader); @EXPORT = qw(); $VERSION = '2.28'; =head1 NAME Form - the HTMLObject::Form class. =head1 SYNOPSIS The HTMLObject::Form module is an attempt to make creating and validating html forms simpler for the programmer and web page designer. This object will use a template provided by the programmer to substitute the generated html form items into. The power behind this module is the fact that the template creator is doing layout, but not actually creating the form items (text fields, select boxes, etc.). These form items are defined in the data structure and profile structures passed into the generate() method so that we can dynamically generate them without having to parse the html template and determine the "field" to update/populate with values. It is much easier to just generate the form item than try to update the html already generated. In an effort to try and reduce duplication of existing code, we are using the Data::FormValidator module to do the form validation code and the HTMLObject object passed into generate() to do form item creation. Your script has to provide 3 things: 1) the HTML template that represents the form being worked on. It must contain the
tags and any layout structure you want. 2) the Data::FormValidator profile structure. 3) the data structure which is an hash of hash refs, where the key is the "name" attribute for the form item being created. See the generate() method for details on the data structure. =head1 EXAMPLE use HTMLObject::Form; use HTMLObject::Normal; use HTMLObject::CGILib; my %commands = ( display => "Display the form", update => "Update the database", ); # build up the data structure to pass to the generate() command # to build up and display the form from. my %data = ( "fname" => { -Label => "First Name:", -Type => "text" }, "lname" => { -Label => "Last Name:", -Type => "text" }, "mname" => { -Label => "Middle Initial:", -Type => "text", size => 1, maxlength => 1 }, "color" => { -Label => "Your Favorite Color:", -Type => "select", -Options => getColors() }, "color2" => { -Label => "All Your Favorite Colors:", -Type => "multi-select", -Options => getColors(), -Value => [ "yellow", "green" ], -Buttons => [ "All", "Toggle", "None" ] }, "color3" => { -Label => "Pick a color:", -Type => "color-picker", -Value => "#000000", }, "startDate" => { -Label => "Start Date:", -Type => "date-picker", -Value => "2004-01-01", -year => "2004", }, "endDate" => { -Label => "End Date:", -Type => "date-picker", -Value => "2004-12-31", -year => "2004", }, "comment" => { -Label => "Comment:", -Type => "textarea" }, "command" => { -Type => "hidden", -Value => "display" }, "formSubmitted" => { -Type => "hidden", -Value => "1" }, ); # create the order array that specifies the order form items should be processed by createTemplate(). my @order = qw( fname mname lname color color2 color3 startDate endDate comment ); # build up the profile to pass to the Data::FormValidator for validation # of the input when the form is submitted back to us. my %profile = ( required => [ qw( fname lname color color2 color3 startDate endDate command formSubmitted ) ], optional => [ qw( mname comment ) ], constraints => { fname => qr/^.+$/, mname => qr/^.$/, lname => qr/^.+$/, color3 => qr/^((#([A-Fa-f0-9]){6})|transparent|inherit)( !important)?$/, command => sub { my $value = shift; return exists $commands{$value}; }, }, ); my $template = <<"END_OF_FORM"; END_OF_FORM my $doc = HTMLObject::Normal->new(); my $formObj = HTMLObject::Form->new(); $doc->setTitle("HTMLObject::Form test script"); # setup the JavaScript error handler support. my $email = "changeme@your.domain"; $doc->enableJavascriptErrorHandler(email => $email, prgName => "form.cgi", prgVersion => "1.0"); use vars qw ( %input %clientFileNames %fileContentTypes %serverFileNames ); HTMLObject::CGILib::ReadParse(*input, \%clientFileNames, \%fileContentTypes, \%serverFileNames); my %form = (); # the variable that holds the generated form data. my $displayForm = 0; # signal when we need to display the form. my $updateData = 0; # signal when the data is ok to work with if (exists $input{formSubmitted}) { # validate the submitted form my $result = $formObj->validate(input => \%input, profile => \%profile); if ($formObj->error) { # display the error message. die $formObj->errorMessage; } if (!$result) { $displayForm = 1; } else { # we have a valid form filled out. # update the database and continue, etc. # use the valid entries from the $formObj instance. $displayForm = 1; $updateData = 1; } } else { $displayForm = 1; } if ($displayForm) %form = $formObj->generate( data => \%data, #template => $template, # uncomment to use the template, for now it is using createTemplate(). name => "mainForm", action => "", method => "post", profile => \%profile, order => \@order); if ($formObj->error) { # display the error message die $formObj->errorMessage; } } if ($updateData) { # output the found entries $form{body} .= "