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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.