diff -ruN Data-FormValidator-4.57.orig/lib/Data/FormValidator/Results.pm Data-FormValidator-4.57/lib/Data/FormValidator/Results.pm
--- Data-FormValidator-4.57.orig/lib/Data/FormValidator/Results.pm 2007-11-01 20:52:02.000000000 -0600
+++ Data-FormValidator-4.57/lib/Data/FormValidator/Results.pm 2008-01-28 12:21:26.000000000 -0700
@@ -237,6 +237,15 @@
}
}
}
+ elsif (ref $deps eq "CODE") {
+ for my $val (_arrayify($valid{$field})) {
+ my $returned_deps = $deps->($self, $val);
+
+ for my $dep (_arrayify($returned_deps)) {
+ $required{$dep} = 1;
+ }
+ }
+ }
else {
for my $dep (_arrayify($deps)){
$required{$dep} = 1;
diff -ruN Data-FormValidator-4.57.orig/lib/Data/FormValidator.pm Data-FormValidator-4.57/lib/Data/FormValidator.pm
--- Data-FormValidator-4.57.orig/lib/Data/FormValidator.pm 2007-11-01 20:52:02.000000000 -0600
+++ Data-FormValidator-4.57/lib/Data/FormValidator.pm 2008-01-28 12:03:03.000000000 -0700
@@ -443,6 +443,15 @@
"pay_type" => {
check => [ qw( check_no ) ],
}
+
+ # if cc_type is VISA or MASTERCARD require CVV
+ "cc_type" => sub {
+ my $dfv = shift;
+ my $type = shift;
+
+ return [ 'cc_cvv' ] if ($type eq "VISA" || $type eq "MASTERCARD");
+ return [ ];
+ },
},
This is for the case where an optional field has other requirements. The
@@ -452,7 +461,12 @@
constraint is added that the optional field must equal a key for the
dependencies to be added.
-Any fields in the dependencies list that is missing when the target is present
+If the dependencies are specified as a code reference then the code will be
+executed to determine the dependent fields. It is passed two parameters,
+the object and the value of the field, and it should return an array reference
+containing the list of dependent fields.
+
+Any fields in the dependencies list that are missing when the target is present
will be reported as missing.
=head2 dependency_groups
diff -ruN Data-FormValidator-4.57.orig/t/dependency_coderef.t Data-FormValidator-4.57/t/dependency_coderef.t
--- Data-FormValidator-4.57.orig/t/dependency_coderef.t 1969-12-31 17:00:00.000000000 -0700
+++ Data-FormValidator-4.57/t/dependency_coderef.t 2008-01-28 12:47:43.000000000 -0700
@@ -0,0 +1,96 @@
+use strict;
+
+$^W = 1;
+
+use Test::More tests => 18;
+use Data::FormValidator;
+
+my %code_results = ( );
+my $input_hashref = { };
+my $input_profile = {
+ dependencies => {
+ cc_type => sub {
+ my $dfv = shift;
+ my $type = shift;
+
+ return [ 'cc_cvv' ] if ($type eq "VISA" || $type eq "MASTERCARD");
+ return [ ];
+ },
+
+ code_checker => sub {
+ my($dfv, $val) = @_;
+
+ $code_results{'code_called'} = 1;
+ $code_results{'num_args'} = @_;
+ $code_results{'value'} = $val;
+ $code_results{'dfv_obj'} = $dfv;
+
+ return [ ];
+ },
+ },
+};
+
+my $validator = Data::FormValidator->new({default => $input_profile});
+my $result;
+
+
+##
+## Validate a coderef dependency
+##
+
+
+## Check that the code actually gets called.
+#############################################################################
+
+$input_hashref->{code_checker} = 'test';
+$result = undef;
+eval { $result = $validator->check($input_hashref, 'default'); };
+
+ok(!$@, "checking that dependency coderef is called");
+ok($code_results{code_called}, " code was called");
+is($code_results{num_args}, 2, " code received 2 args");
+is($code_results{value}, 'test', " received correct value");
+ok($code_results{dfv_obj}, " received dfv object");
+isa_ok($code_results{dfv_obj}, 'Data::FormValidator::Results',
+ " dfv object");
+
+delete $input_hashref->{code_checker};
+
+
+## Value that should cause a missing dependency.
+#############################################################################
+
+$input_hashref->{cc_type} = 'VISA';
+$result = undef;
+eval { $result = $validator->check($input_hashref, 'default'); };
+
+ok(!$@, "checking a value that has a depenency");
+isa_ok($result, "Data::FormValidator::Results", " returned object");
+ok($result->has_missing, " has_missing returned true");
+ok($result->missing('cc_cvv'), " missing('cc_cvv') returned true");
+
+
+## Value that should NOT cause a missing dependency.
+#############################################################################
+
+$input_hashref->{cc_type} = 'AMEX';
+$result = undef;
+eval { $result = $validator->check($input_hashref, 'default'); };
+
+ok(!$@, "checking a value that has no dependencies");
+isa_ok($result, "Data::FormValidator::Results", " returned object");
+ok(!$result->has_missing, " has_missing returned false");
+is($result->missing('cc_cvv'), undef, " missing('cc_cvv') returned false");
+
+
+## Test with multiple values
+#############################################################################
+
+$input_hashref->{cc_type} = [ 'AMEX', 'VISA' ];
+$result = undef;
+eval { $result = $validator->check($input_hashref, 'default'); };
+
+ok(!$@, "checking multiple values");
+isa_ok($result, "Data::FormValidator::Results", " returned object");
+ok($result->has_missing, " has_missing returned true");
+is($result->missing('cc_cvv'), 1, " missing('cc_cvv') returned true");