=head1 NAME
CGI::QuickForm - Perl module to provide quick CGI forms.
=head1 SYNOPSIS
# Minimal example. (Insecure no error checking.)
#!/usr/bin/perl -w
use strict ;
use CGI qw( :standard :html3 ) ;
use CGI::QuickForm ;
show_form(
-ACCEPT => \&on_valid_form, # You must supply this subroutine.
-TITLE => 'Test Form',
-FIELDS => [
{ -LABEL => 'Name', }, # Default field type is textfield.
{ -LABEL => 'Age', }, # Stored in param( 'Age' ).
],
) ;
sub on_valid_form {
my $name = param( 'Name' ) ;
my $age = param( 'Age' ) ;
open PEOPLE, ">>people.tab" ;
print PEOPLE "$name\t$age\n" ;
close PEOPLE ;
print header, start_html( 'Test Form Acceptance' ),
h3( 'Test Form Data Accepted' ),
p( "Thank you $name for your data." ), end_html ;
}
# All QuickForm options (aide memoir)
#!/usr/bin/perl -w
use strict ;
use CGI qw( :standard :html3 ) ;
use CGI::QuickForm ;
show_form(
-ACCEPT => \&on_valid_form,
-BORDER => 0,
-FOOTER => undef,
-HEADER => undef,
-INTRO => undef,
-LANGUAGE => 'en',
-USER_REQUIRED => undef,
-USER_INVALID => undef,
-TITLE => 'Test Form',
-REQUIRED_HTML => '+',
-INVALID_HTML => '*',
-VALIDATE => undef, # Set this to validate the entire record
-SIZE => undef,
-MAXLENGTH => undef,
-ROWS => undef,
-COLUMNS => undef,
-CHECK => 1,
-SPACE => 0, # Output some newlines to assist debugging if 1
-MULTI_COLUMN => 0,
-NAME => undef,
-ONSUBMIT => undef,
-JSCRIPT => {},
-STYLE_FIELDNAME => '',
-STYLE_FIELDVALUE => '',
-STYLE_BUTTONS => '',
-STYLE_ROW => '',
-STYLE_WHY => '',
-TABLE_OPTIONS => '',
-FIELDS => [
{
-LABEL => 'Personal Details',
-HEADLINE => 1,
-STYLE_FIELDNAME => '',
-COLSPAN => 2,
-END_ROW => 1,
},
{
-LABEL => 'Name',
-START_ROW => 1,
-END_ROW => 1,
-COLSPAN => 1,
-REQUIRED => undef,
-TYPE => 'textfield',
-VALIDATE => undef, # Set this to validate the field
-CLEAN => undef, # Set this to clean up valid data
-DESC => undef,
-STYLE_FIELDNAME => '', # If set over-rides form-level setting
-STYLE_FIELDVALUE => '', # If set over-rides form-level setting
-STYLE_ROW => '', # If set over-rides form-level setting
# Lowercase options are those supplied by CGI.pm
-name => undef, # Defaults to -LABEL's value.
-default => undef,
-size => 30,
-maxlength => undef,
},
# For all others: same QuickForm options as above
# and all CGI.pm options (which vary with -TYPE) available
{
-LABEL => 'Address',
-TYPE => 'textarea',
-rows => 3,
-columns => 40,
},
{
-LABEL => 'Password',
-TYPE => 'password_field',
},
{
-LABEL => 'Hair colour',
-TYPE => 'scrolling_list',
'-values' => [ qw( Red Black Brown Grey White ) ],
-size => 1,
-multiples => undef,
},
{
-LABEL => 'Worst Sport',
-TYPE => 'radio_group',
-values => [ qw( Boxing Cricket Golf ) ],
-default => 'Golf',
},
# Any other CGI.pm field can be used in the same way.
],
-BUTTONS => [
{ -name => 'Add' },
{ -name => 'Edit' },
{ -name => 'List' },
{ -name => 'Remove' },
{ -name => 'Clear', -DEFAULTS => 1 },
],
) ;
=head1 DESCRIPTION
C, provides a quick and simple mechanism for providing on-line CGI
forms.
When C executes it presents the form with the fields requested.
As you can see from the minimal example at the beginning of the synopsis it
will default everything it possibly can to get you up and running as quickly
as possible.
If you have specified any validation it will validate when the user presses
the submit button. If there is an error it will re-present the form with the
erroneous fields marked and with all the data entered in tact. This is
repeated as often as needed. Once the user has corrected all errors and the
data is valid then your C<&on_valid_form> subroutine will be called so that
you can process the valid data in any way you wish.
Note that EXAMPLE #1 and EXAMPLE #2 are in this pod; C, C,
etc. are supplied as files.
=head2 QuickForm form-level (record-level) options
=over
=item C<-ACCEPT>
Required subroutine reference. This is a reference to the subroutine to
execute when the form is successfully completed, i.e. once all the fields and
the whole record are valid (either because no validation was requested or
because every validation subroutine called returned true). The parameters are
accessible via C, so your C<&on_valid_form> may look something like
this:
sub on_valid_form {
my $first_param = param( 'first' ) ;
my $second_param = param( 'second' ) ;
my $third_param = param( 'third' ) ;
# Process, e.g. send an email or write a record to a file or database.
# Give the user a thank you.
}
=item C<-BORDER>
Optional integer. This is the border width. Default is zero. You would
normally set this to 1 if you are using C<-DESC> to add textual descriptions
to your fields.
=item C<-BUTTONS>
Optional array reference. This is an array of submit buttons. The buttons
appear at the bottom of the form, after all the fields. Each button is defined
as an anonymous hash, e.g.
-BUTTONS => [
{ -name => 'New' },
{ -name => 'Update' },
],
although any other legitimate C options may also be given, e.g.
-BUTTONS => [
{ -name => 'New', -value => 'BUTTON_NEW' },
{ -name => 'Update', -value => 'BUTTON_UPDATE' },
],
If you want a button which resets the form to its default values then create
an entry like this:
{ -name => 'Clear', -DEFAULTS => 1 },
If no C<-BUTTONS> option array reference is given it will be created with
C<{ -name =E 'Submit' }> by default. Note that this option replaces the
C<-BUTTONLABEL> option. If C<-BUTTONLABEL> is used it will be converted into
the new form automatically so old scripts will I be broken. However use
of C<-BUTTONS> is recommended for all new work. To see which button has been
pressed you might use code like this in your on_valid_form subroutine:
if( param( 'New' ) ) {
# New pressed
}
elsif( param( 'Update' ) ) {
# Update pressed
}
# etc.
=item C<-CHECK>
Optional boolean, default is true. When C is called it will check
(i.e. do validation) providing there are parameters (i.e. the user has filled
in the form) I if C<-CHECK> is true. This option would not normally be
used. However if you have links which call your form with some parameters
(e.g. default values), you will want the form to be displayed with the
defaults but I any validation taking place in the first instance. In
this situation you would set C<-CHECK> to false. Thus we must cope with the
following scenarios:
=over
=item 1.
Form is called with no params - must display blank form and validate when
the user presses a button;
=item 2.
Form is called with params (e.g. by clicking a link we've provided) - must
display form with any defaults and I validate until the user presses a
button;
=item 3.
Form is called with params (as the result of the user pressing a button) -
validation must take place.
=back
To achieve the above we need to add an extra field=value pair to the URL we
provide and if that is present then skip validation. The field's name must
I be one of the form's fields! e.g.
# If it is to be called from one of our own URLs with something like
# www.mysite.com/cgi-bin/myscript?colour=green&size=large
# then we must add in the extra field=value and write the preceeding link
# for example as:
# www.mysite.com/cgi-bin/myscript?QFCHK=0&colour=green&size=large
# We then use query_string() to set -CHECK to 0 and show the form with the
# defaults without validating - we'll validate when they press a button.
# If its been called as something like www.mysite.com/cgi-bin/myscript
# then set -CHECK to 1 which gives us standard behaviour:
# i.e. if there are params then show_form will validate; otherwise it will
# show the blank form.
show_form(
-CHECK => ( query_string() =~ /QFCHK=0/o ? 0 : 1 ),
# etc
) ;
# Or more verbosely:
my $Check = 1 ;
$Check = 0 if query_string() =~ /QFCHK=0/o ;
show_form(
-CHECK => $Check,
# etc
) ;
Note that QuickForm discards any query string if it reinvokes itself because
of invalid data. This is useful because it means you can use the query string
to distinguish between a 'first time' call and subsequent calls as we do here
with -CHECK. However if you want a query string parameter to survive these
calls we must extract them and pass them ourselves, e.g. via a hidden field.
=item C<-FOOTER>
Optional string. This is used to present any text following the form and if
used it must include everything up to and including final "