What I would like to do is use DFV to validate that at least one of a set of HTML form entries (call them foo_1, foo_2, etc.) has been chosen. This may sound like an XY problem, but since I'm already using DFV for other aspects of validation (checking email, checking passwords), this seems like a reasonable thing to attempt.

DFV has profile entries for "require_some" (require N entries from a set of fields) as well as "required_regexp" (specify required fields with a pattern--this is close, but it ends up making all of foo_* required, which is bad). Apparently what I want is "require_some_regexp", where I can specify that I want e.g. one or more of qr/^foo_\d+$/.

I cobbled this constraint together:

sub require_some_regexp { my ($n, $re) = @_; return sub { my $dfv = shift; $dfv->name_this('require_some_regexp'); my $count = grep /$re/, keys %{ $dfv->get_filtered_data }; return $count >= $n; }; }
... but then I realized that in the "constraint_methods" portion of the profile, the keys are the (hardcoded) form fields to be constrained, so that's not going to work. I could tie it to some hidden field that's guaranteed to be submitted, but that's just a workaround.

There's also "constraint_method_regexp_map", but again I don't know ahead of time what the fields will be. I could use regexp qr/./, but then what happens if 99 foo_* fields have values? My validation code (which needs to be run only once) will be run 99 times, which is suboptimal.

I'm starting to think that DFV just can't do it. I've even looked at the source, thinking I could plug in a method or three, but the "check" method is just a thin wrapper to the DFV::Results object constructor, which is 300 (dense) lines long, I kid you not. So I'm not too keen on doing anything in there.

TIA for all the help I know I'm going to get here.


In reply to Data::FormValidator, equivalent to "require_some_regexp"? by trammell

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.