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

Monks, I am using Data::FormValidator to provide input checking/constraints/filters for my CGI::Application.

Data::FV makes it easy to code constraints on $B and $C, or to require $B and $C if $A exists by setting up rules contained in a hash structure. Like so...

# snip dependencies => { # If A is "N", make list of inputs required "A" => { N => [ qw/ admin_contact_fk + tech_contact_fk billing_contact_fk / ], } }, constraints => { # check these inputs with these subs dn_id => 'OK_int', foo => 'OK_chars', }, # snip

I would like to write a construct so that if Input $A = exists, then Input $B and $C have to meet certain constraints. If $A isn't one of the inputs, then the $B anc $C have no constraints

I don't see any way other way, using D::FV, than writing custom constraints that take in $A, $B, and $C to solve my problem. Is this true?

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

Replies are listed 'Best First'.
Re: Data::FormValidator question
by freddo411 (Chaplain) on Mar 18, 2004 at 18:56 UTC
    Strangely, I'm replying to myself:

    The answer to my question is yes, one must write a custom constraint. I got this reply from the module author (Mark Stosberg):

    Fred, if using the 'dependencies' system doesn't solve your problem, I think you are right. You could write a custom constraint for b or c, such that they only kick-in if A is present.

    He also suggested that I propose any new functionality to the maintainers list.

    Solving my problem I used the following code (see Data::FormValidator for explaination)

    # .. snip # Part of my hash to pass to D::FV constraints => { dn_id => 'OK_int', b => { constraint => 'custom_constraint_sub_name', params => [ qw/ b a / ], }, c => { constraint => 'custom_constraint_sub_name', params => [ qw/ c a / ], }, } # .. snip ... # Defining my constraint routine elsewhere: ################################# sub match_custom_constraint_sub_name { my $val = shift; my $a = shift; if ( $a eq 'N' ) { return match_another_custom_constraint($val) ; } else { return "0"; # use this default value }

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