Hello,
I just came up with some new code to integrate CGI::App and
Data::FormValidator more closely. If other folks have some interest it,
and I'll genericize the the final bit for public consumption and release
it. (As it is, the snippets below are not a complete solution). Here's
the summary of what it does:
Most of the time when I validate a form what I want to do is immediately
return the form with a problem, re-filling the form with the users data
and marking the problem fields with red text: "* Invalid" or "*
Missing". My code helps automate this piece, using Data::FormValidator
and HTML::FillInForm. Here's how you would call it in a CGI::App run
mode:
my ($valid, $err_page) = $self->validate_rm(
'real_estate_search_form',_real_estate_search_profile()
);
return $err_page if $err_page;
#######
So there are two inputs: the name of the run mode that you want to
return if there are any errors, and a D::FV profile definition. Here it
will be returned by a subroutine.
It returns the "valid" hash, as well as the error page, prefilled with
errors marked.
There is just a wee bit more work you have to do cooperate with the
system. Your run mode that may have errors has to be prepared for that.
In the code you have add lines like this:
sub real_estate_search_form {
my $self = shift;
my $errs = shift;
my $t = $self->load_tmpl('file.tmpl');
$t->param($errs) if $errs;
}
As well, you have to tokens in your template to display errors, like:
<!-- tmpl_var err_field name -->
The token "err__" will also be set just to let you know "There are some
errors". This might be used to trigger a general message at the top of
the page. If other templating systems (besides HTML::Template) can't
acceept a hashref of data, they may need a slightly different
implementation.
Here's the details of how the routine is working. The "error_marks"
routine isn't generic enough for general release yet.
########
sub validate_rm {
my $self = shift;
my $return_rm = shift || die 'validate_rm: missing required input argument';
my $profile = shift || die 'validate_rm: missing required input argument';
require Data::FormValidator;
my $v = new Data::FormValidator({ profile => $profile });
my ($valid,$missing,$invalid) = $v->validate($self->query, 'profile');
# if there are errors, prepare an error page;
my $err_page;
if (@$missing or @$invalid) {
my $return_page = eval '$self->'.$return_rm.'(error_marks($missing, $invalid)
)';
die "validate_rm: invalid return_rm: $@" if $@;
require HTML::FillInForm;
my $fif = new HTML::FillInForm;
$err_page = $fif->fill(
scalarref => \$return_page,
fobject => $self->query
)
}
return ($valid,$err_page);
}
#####
Are other folks interested in this routine, either as apart of CGI::App or in a
utility module?
Mark
--
http://mark.stosberg.com/
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/[EMAIL PROTECTED]/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]