I process a lot of forms with CGI:::Application. It seems like the primary
thing I do with it. The ::ValidateRM plugin is great, but it seems like I'm
still writing the same patterns of code over and over, and things could be
further simplified.
I propose the following solution to improve things:
1. A new method "form_modes" will be used as an alternative to run_modes()
to declare a name that represents a pair of run modes: one to display a
form and one to process it:
$c->form_modes(qw/
my_form'
/);
2. If 'my_form' run mode does not exist, one is created like this:
sub my_form {
my $c = shift;
my $errs = shift;
my $t = $c->load_tmpl;
$t->param($errs) if $errs;
return $t->output;
}
That's the basic code to load a template that supports passing through
the error messages like check_rm() expects.
3. We require 'my_form_profile' method to exist and return a DFV profile.
4. 'my_form_validate' is created for you with check_rm() functionality. This
is where your form should submit. If the form validation fails, it does the
check_rm() thing and returns the original form with errors
5. If the form validation succeeds, it forwards on to the "my_form_process"
run mode.
Putting it all together, here are "before" and "after" versions showing how
the code is simplified when using this feature:
sub setup {
my $c = shift;
$c->run_modes([qw/
my_form
my_form_process
/);
}
sub my_form {
my $c = shift;
my $errs = shift;
my $t = $c->load_tmpl;
$t->param($errs) if $errs;
return $t->output;
}
sub my_form_process {
my $c = shift;
my ($results,$err_page) = $c->check_rm('my_form', 'my_form_profile');
return $err_page if $err_page;
# ...
}
### After ############
sub setup {
my $c = shift;
$c->form_modes([qw/
my_form
/);
}
sub my_form_process {
my $c = shift;
# Now the form is now validated when we arrive here!
# ...
}
########################
If you have a more complicated case, you can define your own 'my_form'
subroutine and still use this module, or just fall back to
using ::ValidateRM if you have even more specialized needs.
Comments?
Mark
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[email protected]/
http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]