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

Curious about Data::FormValidator I created a short test case. It's not working and two hours later I'm at a loss.

Here's what I have (in a CGI::Application context)...
sub validate_orderform { my $self = shift; my $q = $self->query(); my $profile = { required => [qw(bill_fname bill_lname bill_street1 bill_city bill_state bill_zip bill_country payment_type)] }; my ($valids, $missings, $invalids, $unknowns) = Data::FormValidator->validate($q, $profile); use Data::Dumper; my $html_output = Dumper($valids, $missings, $invalids, $unknowns) +; return $html_output; }

I get the following output which seems to indicate that all the fields are parsed as valid despite the fact that two of the required fields (bill_lname and bill_zip) were left blank.The missing required fields are not listed among the $missings ($VAR2).

$VAR1 = {"bill_country" => ["USA"],"bill_lname" => [""], "bill_fname" => ["John"],"bill_street1" => ["123 Main"], "bill_state" => ["NY"],"bill_city" => ["New York"], "bill_zip" => [""],"payment_type" => ["Check"]}; $VAR2 = []; $VAR3 = []; $VAR4 = [".cookies",".fieldnames",".charset",".parameters","action"];
Can any Data::FormValidator users shed some light on this? Thanks!

sz

Replies are listed 'Best First'.
Re: Data::FormValidator, Not
by impossiblerobot (Deacon) on Sep 19, 2002 at 17:37 UTC

    Unfortunately, at this time you cannot pass a CGI object into Data::FormValidator. You must pass a reference to a hash containing your form data, which you can easily get by calling:

    my $formdata = $q->Vars; my ($valids, $missings, $invalids, $unknowns) = Data::FormValidator->validate($formdata, $profile);

    Oddly enough, I just finished writing a patch to D:FV that would allow passing a CGI object; I just haven't gotten around to submitting it to the module maintainer (markjugg).


    Impossible Robot
      Thanks so much! It works fine now. I don't know where I got the idea that I could pass a CGI object. I've probably gotten too used to that convenience with other modules I use.

      Your patch would make a great addition!

      sz
      Of course, you can skip the temporary variable using
      my ($valids, $missings, $invalids, $unknowns) = Data::FormValidator->validate( scalar($q->Vars), $profile);
      Do those couple extra characters warrant a patch? Shrug.. Well, YMMV. Update: it obviously does - something new learned for the day.

      Makeshifts last the longest.

        Although eliminating the temporary variable would work for the simplest case, Data::FormValidator does more than just validate data; it also applies filters to the data. Without the temporary variable, you're throwing away your filtered data.

        Therefore, the need for the patch: to filter the CGI object data directly, rather than in a separate data structure. :-)


        Impossible Robot
      impossiblerobot, I'll be interested to see your patch. One 'gotcha' with using the direct CGI object is that it supports multiple values for a single variable. Data::FormValidator does not currently support this, though perhaps it should.

      To provide that functionality, we have to figure out if all the values should receive the same validation (that seems reasonable), and how we report the errors if some values fail but not others.

      Then to be consistent, we should probably also support a way to have multi-values when passing a regular hash, perhaps with a key that points to an array reference containing multiple values.

      -mark

Re: Data::FormValidator, Not
by knowmad (Monk) on Sep 19, 2002 at 20:49 UTC
    Sz,

    You've obviously gotten past your block but you may want to check out the Data::FormValidator::Tutorial package on CPAN for some additional tips to using this module. It's a great tool.

    I'm trying to develop a way to add untaint support since this module already provides 99% of what taintmode is intended to cover. Enjoy D::FV and be sure to join the mailing list at http://lists.sourceforge.net/lists/listinfo/cascade-dataform.

    William

Re: Data::FormValidator, Not
by princepawn (Parson) on Sep 27, 2002 at 01:02 UTC