Hagbone has asked for the wisdom of the Perl Monks concerning the following question:

Brethren .....

I'm preparing to move to some sort of global form input validation, and after searching this site, and cruising CPAN for a bit, I'm coming up with DATA::FormValidator and HTML::FormValidator as potential CPAN modules worth considering.

I'm curious if anyone has had experience with both, and would be even more interested in hearing from those who have and understanding of the difference between the two modules above.

One item I'd like the module to accommodate is relative flexibility in crafting the messages that go along with each form field that gets spit back during validation.

DATA::FormValidator has an INPUT PROFILE SPECIFICATION: "msg" that apparently deals with customizing error messages, but also includes this: "NOTE: This part of the interface is newer and may change. Use in production code at your own caution." .... HTML::FormValidator apparently doesn't have as robust (or any) error message customization ability.

Any insights would be appreciated ... maybe there's other approaches I should be considering? It seems rolling my own would not be the best use of time in this situation (*nix).

Replies are listed 'Best First'.
Re: Form validation: preferred modules?
by freddo411 (Chaplain) on Dec 17, 2003 at 18:29 UTC
    I've been very successful using Data::FormValidator along with CGI::Application::ValidateRM and HTML::Template. With these it becomes a breeze to write custom validations and untainting routines , return specific, customized, and context specific error messages.

    What's more, the HTML template system allows the designer to position and style the error message. Also, the custom error messages can be kept in seperate file for easy editing (I use Config::General for loading and reading the file). My error config file looks like this:

    <msgs> any_errors = err__ prefix = err_ <constraints> OK_dn_unique = Domain name already used within Citigroup OK_dn = Badly formed domain name OK_words = Illegal character in string OK_chars = Illegal character or space in string OK_sql = Illegal SQL character in string OK_phone_chars = Illegal phone character(s) or format OK_int = Illegal integer OK_date = Illegal date character(s) or format OK_email = Illegal email format </constraints> </msgs>

    The key of the constraints hash in the function name of the constraint. Details are explained clearly in the validateRM docs. Here's an example validate/untaint function I wrote (note how simple it is):

    sub match_OK_phone_chars { my $val = shift; if ( $val =~ /^([-\d. \(\)]{10,}[extEX .-\d]*)$/ ) { return $1; } else { return undef; } }
    I highly recommend using these modules.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: Form validation: preferred modules?
by jdtoronto (Prior) on Dec 17, 2003 at 16:22 UTC
    DATA::FormValidator.

    I use it in conjunction with CGI::Application::ValidateRM. DFV allows custom relationships to be enforced, grouping of fields, and fully customisable messages. These modules all work extremely well together.

    HTML::FormValidator is the predecessor to Data::FormValidator, you will notice the intense similarity. HTML::FV has not been maintained in a year or so, Data::FV is actively maintained by Mark Stosberg and is based on the HTML::FV code.

    update CGI::Untaint is relatively new and comes from Tony Bowden, the author of Class::DBI, as yet it has limited validations. I have not tried it. Params::Validate would seem to be more useful in validating command line parameters rather than form input.

    jdtoronto

      I have some (vague) misgivings about combining untainting and validation as CGI::Untaint and others do. In some cases you would want input to remain tainted, even after it's validated.

      That said, I don't use taint mode very often so I'm not is a good position to judge the risk-convenience balance.

Re: Form validation: preferred modules?
by hanenkamp (Pilgrim) on Dec 17, 2003 at 16:17 UTC
Re: Form validation: preferred modules?
by bsb (Priest) on Dec 22, 2003 at 08:18 UTC
    There's also CGI::FormBuilder with does validation and much else besides. I found it a little bloated and too proscriptive. YMMV