Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: TemplateToolkit and CGI Forms

by Anneq (Vicar)
on Oct 23, 2004 at 02:40 UTC ( #401755=note: print w/replies, xml ) Need Help??

in reply to TemplateToolkit and CGI Forms

Here is what I do to use one template form for submitting the form and for processing results. I use a default (or no) run mode for the initial form display. When the form is submitted, the form's action url changes the run mode to a specific one for handling the submitted form. I make use of a separate CGI parameter (e.g., page) to indicate the resource (template page) to be accessed. This way, I can reuse the same run mode names but for different situations, for example, add a record to different databases. The action run mode could be 'add_record', while the resource may be 'some_section.employee.add_record' or 'some_section.inventory.add_record'. There is a methods module for each database that handles the required database actions.

A template component handling the form input and result might look like:

[% IF msg == 'success' %] <p>You were successful.</p> [% ELSE %] [% msg IF msg %] ... show the form because something went wrong ... [% END %]

The CGI::Application run modes would look like:

$self->run_modes( 'default' => 'default',\ 'create_database' => 'database_action', 'add_record' => 'database_action', 'delete_records' => 'database_action', 'modify_record' => 'database_action', 'other_run_mode' => 'other_sub', );

In my CGI::Application module, here is the run mode sub:

sub database_action { my $self = shift; my $q = $self->query(); my $me = "$self->database_action()"; # Used in result messages for + debugging my @page = split('\.', $q->param('page')); # Resource requested, f +or example, page='section.inventory.add_record' my $database_name = $page[$#page - 1]; # The second-last page elem +ent is the database my $database_action = $page[$#page]; # The resource as well as the + method # Do some things here relating to the run mode my $pkg = "My::SomePath::$database_name"; eval "require $pkg;"; if ($@) { $result = "<p>$me: Failed to require $pkg.</p> <p>\$\@ says: $@ and <br/>\$! says: $! and<br/>\$result: $resu +lt</p>"; } my $method_call = "$pkg->${database_action}(\$self)"; $result .= eval "$method_call;"; if ($@) { $result .= "<p>$me: $method_call failed.</p> <p>\$\@ says: $@ and <br/>\$! says: $! and<br/>\$result is: $resul +t</p>"; } my $output .= My::CGIOut->build_page($self, $result); # Process th +e template return $output; }

And in a module (My::SomePath::SomeDB) to handle reusable code used by the run mode:

sub do_some_database_action { my $self = shift; my $cgi_app = shift; my $q = $cgi_app -> query(); my $session = $cgi_app->param('session'); my $result = ''; my $me = "$self->do_some_some_database_action()"; # Used in result + messages for debugging #----------------------------------------------------------------- +---------- # Do Something with query parameters like Data::FormValidator #----------------------------------------------------------------- +---------- my $result = My::OtherModule->validate_something($cgi_app); return $result if ($result); #----------------------------------------------------------------- +---------- # Do Something if successful #----------------------------------------------------------------- +---------- #----------------------------------------------------------------- +---------- # Finish up #----------------------------------------------------------------- +---------- $result = 'success' if (!$result); return $result; }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://401755]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-06-28 16:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (640 votes). Check out past polls.