I'm using Data::FormValidator, which has saved me many lines of code and has greatly simplified my validation logic. Excellent module.

However, due my lack of Perl experience, I'm having trouble creating a D::FV profile to apply multiple constraints to a single query parameter. I've searched the POD, perlmonks and googled but didn't find anything that helped me see how to do it. Here is a snippet of what I've got so far:

my $ca_obj = shift; # CGI::Application object my $q = $ca_obj->query(); my $dfv_profile = { required => [qw (userid)], constraints => { userid => { name => 'userid_exists', constraint => sub { return !MyDBITools->user_exists($ca_obj); }, }, userid => { name => 'userid_untaint', constraint => qr/^[-\@\w.]+$/, }, }, msgs => { format => '%s', constraints => { 'userid_exists' => "User ID already exists.", 'userid_untaint'=> "User ID contains bad stuff", }, }, }; my $results = Data::FormValidator->check($q, $dfv_profile);

Using the code above, as I fully expected it to, the userid_untaint constraint is overriding the userid_exists constraint, because the second instance of the userid hash key is trashing the first. This is clear from my results. I get the "User ID contains bad stuff" error and the already existing user gets registered. If I remove the userid_untaint constraint, I get the expected error message "User ID already exists."

Does anyone know the proper syntax to write multiple constraints for a single variable?

Thanks,

Anne


In reply to Mulitple constraints for Data::FormValidator by Anneq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.