hesco has asked for the wisdom of the Perl Monks concerning the following question:

Good morning.

Yesterday I posted my confusion with dispatch issues while using CGI::App and CGI::FormBuilder. The code below is a runnable sample, I've written in parrallel, largely by copying / adapting from perldoc, to the nearly complete script that's waiting for me to figure this piece out.

This script only seems to dispatch to pages with required fields in their forms. Otherwise it jumps straight from registrant_form (where it collects a name and state) to payment form (where it collects credit card). And when I make a field required on a page's form, it only stops on that form due to a validation error, not as if on its first pass through.

Here is the 8 line cgi script that uses the module:

#!/usr/bin/perl -wT use strict; use lib qw{/path/to/sandbox/Registration/Registration/t/}; use dispatch_test; my $dispatch = dispatch_test->new(); $dispatch->run(); 1;
And here is the 130 or so line test module:

package dispatch_test; use strict; use warnings; use diagnostics; use CGI::Carp qw/fatalsToBrowser/; use base 'CGI::Application'; use CGI::FormBuilder; use CGI; use Data::Dumper; 1; sub setup { my $self = shift; # my $rm = $form->cgi_param('rm'); $self->start_mode('RegForm'); $self->mode_param('rm'); $self->run_modes( 'RegForm' => 'registrant_form', 'RegistrantNeeds' => 'registrant_needs', 'Checkout' => 'checkout', 'Payment' => 'payment', 'Thankyou' => 'thank_you', 'Admin' => 'admin_screen', ); } sub registrant_form { my $self = shift; print STDERR "dispatch_test: Now running registrant_form().\n"; my $output = ""; my $q = new CGI; my $rm = $q->param('rm') || 'RegForm'; my $form = CGI::FormBuilder->new( fields => [qw/fname lname state/], required => [qw/fname lname state/], params => $q, # params from CGI.pm keepextras => 1, # keep mode param submit => [qw/Proceed_to_Meals/], ); if($form->submitted && $form->validate){ $rm = 'RegistrantNeeds'; # $output = $form->confirm(header => 1); return $self->registrant_needs(); } else { $output = $form->render(); } print STDERR "dispatch_test->registrant_form() says \$rm is $rm.\n"; return $output; } sub registrant_needs { my $self = shift; print STDERR "dispatch_test: Now running ->registrant_needs().\n"; my $output = ""; my $q = new CGI; my $rm = $q->param('rm') || 'RegistrantNeeds'; my $form = CGI::FormBuilder->new( fields => [qw/housing meals other/], required => [qw/meals/], params => $q, # params from CGI.pm keepextras => 1, # keep mode param submit => [qw/Proceed_to_Checkout/], ); if($form->submitted && $form->validate){ $rm = 'Checkout'; return $self->checkout(); } else { $output = $form->render(); } return $output; } sub checkout { my $self = shift; print STDERR "dispatch_test: Now running ->checkout().\n"; my $output = ""; my $q = new CGI; my $rm = $q->param('rm') || 'Checkout'; my $form = CGI::FormBuilder->new( fields => [qw/donation/], required => [qw/donation/], params => $q, # params from CGI.pm keepextras => 1, # keep mode param submit => [qw/Proceed_to_Payment/], ); if($form->submitted && $form->validate){ $rm = 'Payment'; return $self->payment(); } else { $output = $form->render(); } return $output; } sub payment { my $self = shift; print STDERR "dispatch_test: Now running ->payment().\n"; my $output = ""; my $q = new CGI; my $rm = $q->param('rm') || 'Payment'; my $form = CGI::FormBuilder->new( fields => [qw/creditcard/], required => [qw/creditcard/], params => $q, # params from CGI.pm keepextras => 1, # keep mode param ); if($form->submitted && $form->validate){ $rm = 'Thankyou'; return $self->thank_you(); } else { $output = $form->render(); } return $output; } sub thank_you { return "Thanks for your registration.<br>We look forward to seeing y +ou."; }
This is only my second attempt to put CGI::FormBuilder and CGI::Application together on a project. Apparently there is something I am missing in this equation. Any insight, direction, working sample code, etc., etc., would be appreciated. Thank you.

-- Hugh

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: seeking further clarity on dispatch logic w/ CGI::Application & FormBuilder
by jdtoronto (Prior) on May 19, 2006 at 15:49 UTC
    hesco,

    It is now some time (about three years) since I tried using CGI::FormBuilder with CGI::Application. I stopped doing it, because in part the two modules fight with each other. Despite the protestations of the author of CGI::FormBuilder the creation of the actual forms is not so onerous. The far more diffcult part is handling the validation. Now whilst CGI::FormBuilder will do some of this for you, it is hard to integrate it into CGI::Application.

    I have found, through several rather large Webapps - all based on CGI::Application that the preferable method is to use Data::FormValidator through the excellent CGI::Application::Plugin::ValidateRM module which has a tight and consistent interface within CGI::Application.

    As Sam Tregar has already suggested, the use of your if .. else statements within a CGI::Application runmode sends shivers up my spine and suggests that you have two dispatch mechanisms at work here.

    In its place as a Form Handler for small stand-alone application CGI::FormBuilder is good. But as part of a framewaork using CGI::Application I am not sure.

    jdtoronto