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; }

Anne


In reply to Re: TemplateToolkit and CGI Forms by Anneq
in thread TemplateToolkit and CGI Forms by bibo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.