Skip Menu |

This queue is for tickets about the Data-FormValidator CPAN distribution.

Maintainer(s)' notes

This is the bug queue for Data::FormValidator.

Report information
The Basics
Id: 24935
Status: resolved
Priority: 0/
Queue: Data-FormValidator

People
Owner: MARKSTOS [...] cpan.org
Requestors: bcbailey [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: patch: coderef for dependencies
Allow the option of a coderef for a dependency, so that more sophisticated dependencies can be made. Attached patch for 4.50. Thanks, Bradley C Bailey
Subject: data-formvalidator-code-dependency-4.50.patch
--- FormValidator.pm.orig 2006-12-04 19:38:14.000000000 -0700 +++ FormValidator.pm 2007-02-13 15:54:16.000000000 -0700 @@ -442,6 +442,15 @@ "pay_type" => { check => [ qw( check_no ) ], } + + # if cc_type is VISA or MASTERCARD require CVV + "cc_type" => sub { + my $type = shift; + + return [ 'cvv' ] if ($type eq "VISA" || $type eq "MASTERCARD"); + return [ ]; + }, + }, This is for the case where an optional field has other requirements. The @@ -451,6 +460,10 @@ constraint is added that the optional field must equal a key for the dependencies to be added. +If the dependencies are specified as a code reference then the code is +executed to determine the dependent fields. It is passed one parameter, +the value of the field, and it should return an array reference. + Any fields in the dependencies list that is missing when the target is present will be reported as missing. --- FormValidator/Results.pm.orig 2006-12-04 19:38:14.000000000 -0700 +++ FormValidator/Results.pm 2007-02-13 15:54:16.000000000 -0700 @@ -237,6 +237,15 @@ } } } + elsif (ref $deps eq "CODE") + { + my $value = (_arrayify($valid{$field}))[0]; + my $returned_deps = $deps->($value); + + foreach my $dep (_arrayify($returned_deps)) { + $required{$dep} = 1; + } + } else { foreach my $dep (_arrayify($deps)){ $required{$dep} = 1;
From: bcbailey [...] cpan.org
Mark, Per our discussion on IRC recently, I have attached an updated patch against 4.57. The coderef now receives a copy of the DFV object as the first parameter. Patch includes documentation and tests. Regards, Bradley C Bailey
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");
Subject: Re: [rt.cpan.org #24935] patch: coderef for dependencies
Date: Mon, 28 Jan 2008 15:08:02 -0500
To: bug-Data-FormValidator [...] rt.cpan.org
From: Mark Stosberg <mark [...] summersault.com>
Bradley C Bailey via RT wrote: Show quoted text
> Queue: Data-FormValidator > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=24935 > > > Mark, > > Per our discussion on IRC recently, I have attached an updated patch > against 4.57. The coderef now receives a copy of the DFV object as the > first parameter. > > Patch includes documentation and tests.
Thanks, Bradley. I'll try to review and release this soon. Mark
This has been reviewed and applied. I expect to a new release in the next three days. Thanks for the submission! Mark