trammell has asked for the wisdom of the Perl Monks concerning the following question:
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:
... 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.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; }; }
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Data::FormValidator, equivalent to "require_some_regexp"?
by Herkum (Parson) on Apr 20, 2007 at 12:37 UTC | |
by trammell (Priest) on Apr 20, 2007 at 14:13 UTC |