Personally, though I use and love Data::FormValidator, I wouldn't use this
particular approach, and wouldn't want it built into CGI::App by default.
Primarily because this approach embeds the generation of the error page
inside the validation function.  I prefer for the error output to be
controlled inside of the run mode, after obtaining the results from
Data::FormValidator.  This seems a lot more modular, flexible and easy to
maintain to me.   For instance, if the run mode matters in the type of
error output you produce, in the approach you suggest you'd either have to
munge the error page output inside the run mode method, or else create your
own validate_pm() to replace the default version.  The same is a problem if
any lexical variables from the scope of the run mode method should
influence the error output.

Also, I have a problem with the structure of the code, although I think it
may be easily fixed.  I find myself having two points where error output
gets produced: right after the form is submitted, using Data::FormValidate,
and after the values are checked against a database.  That means that the
error page producing code (for your case, the bit after '# if there are
errors, prepare an error page;') needs to be set apart in it's own method
so it can be invoked in different contexts.

In any case, I vote that anything like this should be put into a utility
module (maybe CGI::Application::Validator) and not directly in CGI::App by
default.   I don't like the idea of building into CGI::App even more
assumptions about what modules you must use (adding HTML::FillInForm and
Data::FormValidate to HTML::Template), and even what tokens must exist in
your template.  It just strikes me as very anti-TMTOWTDI.

-- Mike F.



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]







---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/[EMAIL PROTECTED]/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to