CGI-QuickForm

 view release on metacpan or  search on metacpan

eg/example6  view on Meta::CPAN

# This module may be used/distributed/modified under the LGPL.
# 
# I can be contacted as <summer@perlpress.com> -
# please include the word 'quickform' in the subject line.
#
# Please note however that if your problems are due to errors like this:
#
#   [error] Undefined subroutine &Apache::QuickTest::handler called.
#
# then the problem is with YOUR CONFIGURATION not with this example or
# QuickForm, (I know because I've had endless problems with this myself), and
# I *cannot* help you!
#
# 
# 
#                           IMPORTANT NOTES
#
#
# To make this example work you MUST do all the following:
#
# 1. Rename this as something.pm, e.g. QuickTest.pm
# 2. Copy it into an Apache subdirectory in your @INC path
# 3. Edit your Apache httpd.conf (or perl.conf) to add a Location for this
#    example, e.g. (assuming you've renamed it QuickTest.pm):
#
# 		<Location /mod_perl/quicktest>
# 		    SetHandler      perl-script
# 		    PerlHandler     Apache::QuickTest
# 		</Location>
# 		
# You can then invoke the script as http://localhost/mod_perl/quicktest
# (You can of course use any location you like; but it should *not* be a real
# path on your machine!)
#
# If you are converting an existing script that uses QuickForm for pure
# mod_perl, (i.e. not for Apache::Registry), then if you use url() as a link
# back to the form, perhaps with extra parameters, e.g. 
#   url() . '?MyParam1=ONE'
# you will need to change this to
#   url() . path_info() . '?MyParam1=ONE'
# compare this example to example2 in this regard in the -FOOTER setup of
# their show_form() calls.
#
# You will also need to add the following lines at the beginning:
#
#       require 'CGI/Apache.pm' ;
#       use Apache::Constants qw( :common ) ;
#
# Howling at the moon may also help, but if you're not in good voice mod_perl
# comes with *excellent* documentation both in print and free on-line -- visit
# http://perl.apache.org

use strict ;

require 'CGI/Apache.pm' ;   # ADDED FOR MOD_PERL
use Apache::Constants qw( :common ) ;   # ADDED FOR MOD_PERL
use CGI qw( :standard :html3 ) ;
use CGI::QuickForm qw( show_form colour ) ;

my $STYLE_FIELDVALUE = 'style="background-color:#DDDDDD"' ;
my $STYLE_HEADLINE   = 'style="background-color:black;color:white;font-weight:bold"' ;

sub handler { # ADDED FOR MOD_PERL

    show_form(
        -MULTI_COLUMN     => 1, # MULTI_COLUMN
        -SPACE            => 0,
        -CHECK            => ( query_string() =~ /QFCHK=0/o ? 0 : 1 ), 
        -FOOTER           => qq{<a href="} . #" 
                             url() . path_info() .  # ADDED FOR MOD_PERL
                             qq{?QFCHK=0\&Age=28\&Surname=Bloggs\&Forename=Freda">} .#"
                             qq{Defaults</a>} . 
                             end_html,
        -BUTTONS          => [ { -name => 'Add' }, { -name => 'Del' } ],
        -STYLE_BUTTONS    => 'center',
        -TITLE            => 'Test Form',
        -ACCEPT           => \&on_valid_form, 
        -SIZE             => 25,
        -STYLE_FIELDNAME  => 'style="background-color:#AAAAAA"',
        -STYLE_FIELDVALUE => $STYLE_FIELDVALUE,
        -STYLE_WHY        => 'style="font-style:italic;color:red"',
        -STYLE_DESC       => 'style="color:darkblue"',
        -VALIDATE         => sub { 
                ( int rand(2), "<P>" . colour( 'RED', 'Randomly Invalid' ) ) },
        -FIELDS     => [
            {
                -LABEL           => 'Personal Information',
                -END_ROW         => 1,
                -HEADLINE        => 1,
                -STYLE_FIELDNAME => $STYLE_HEADLINE,
                -COLSPAN         => 4,
            },
            {
                -LABEL    => 'Forename',
                -CLEAN    => \&cleanup, 
                -VALIDATE => sub { 
                                my $valid = length shift > 3 ; 
                                ( $valid, '<br />Name too short' ) ;
                            }
            },
            {
                -LABEL    => 'Surname',
                -END_ROW  => 1, # MULTI_COLUMN
                -CLEAN    => \&cleanup, 
                -REQUIRED => 1,
                -VALIDATE => sub { 
                                my $valid = length shift > 3 ; 
                                ( $valid, '<br />Name too short' ) ;
                            }
            },
            {
                -LABEL => 'Sex',
                -TYPE  => 'radio_group',
                '-values' => [ qw( Female Male ) ],
            },
            {
                -LABEL    => 'Age',
                -END_ROW  => 1, # MULTI_COLUMN
                -VALIDATE => &mk_valid_number( 3, 130 ), 
                -size     => 10,
            },



( run in 1.698 second using v1.01-cache-2.11-cpan-39bf76dae61 )