http://www.perlmonks.org?node_id=507565

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

Hello monks-of-unusual-size,

I've got a bit of a situtation with Data::FormValidator and a form... not sure what to do.

The form contains a checkbox called same_as_billing. If same_as_billing is checked that means re-use the billing address as the shipping address. Otherwise, I have to get shipping address information. That means I want Data::FormValidator to make some other fields required. Essentially, I want the inverse of the dependencies clause (since undef is not a valid hash key). So I know I can't do something like this:

dependencies => { 'same_as_billing' => { undef => [ qw/street1 street2 city/ ] }, }

I realize I could flip the meaning of the checkbox to be different_from_billing and I could easily do something like this:

dependencies => { different_from_billing => [ qw/street1 street2 city/ ], }

... but Marketing dislikes the wording. Yay.

Manually tinkering with the POST/GET var before using Data::FormValidator is one way I could get around this by getting the inverse value but that seems messy and could confuse someone just looking at the code. Is there a way I can do this purely using DFV?

Many thanks,
meraxes

Replies are listed 'Best First'.
Re: Data::FormValidator inverse dependencies?
by philcrow (Priest) on Nov 10, 2005 at 22:08 UTC
    Could you switch to constraint_methods for the billing fields? I haven't done this, so I'm not going to give an example. But look in the perldoc for Data::FormValidator in the section called VALIDATING INPUT BASED ON MULTIPLE FIELDS. It refers to the perldoc for Data::FormValidator::Constraints and its section WRITING YOUR OWN VALIDATION ROUTINES.

    I think those sections and a bit of play will work.

    Essentially, you write your own validation routines for your billing address fields. Each of those routines can resceive the value of the checkbox as a parameter.

    Phil