Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Problems with not so trivial data validation using Data::FormValidator

by ted.byers (Monk)
on Jan 31, 2013 at 22:43 UTC ( #1016409=perlquestion: print w/replies, xml ) Need Help??
ted.byers has asked for the wisdom of the Perl Monks concerning the following question:

Maybe I am cognitively challenged, but I thought I did what the documentation says to do if there are multiple constraints to apply to a given field. To be clear, here is what the documentation says (a direct quote, but I don't see anything in the interface for editing this that lets me distinguish between such a quote and what I write)


Multiple constraints can be applied to a single field by defining the value of the constraint to be an array reference. Each of the values in this array can be any of the constraint types defined above.

When using multiple constraints it is important to return the name of the constraint that failed so you can distinguish between them. To do that, either use a named constraint, or use the hash ref method of defining a constraint and include a name key with a value set to the name of your constraint. Here's an example:

my_zipcode_field => [ 'zip', { constraint => '/^406/', name => 'starts_with_406', } ],

You can use an array reference with a single constraint in it if you just want to have the name of your failed constraint returned in the above fashion. Read about the validate() function above to see how multiple constraints are returned differently with that method.

Now here is what I tried:

ccexpiry => [ 'ccexp', { constraint => sub { my ($dfv,$val) = @_; return $val =~ m/^\d{4}$/, }, name => 'must be_4_digits' }, { constraint => after_today('%m%y'), name => 'card_expired' } ],

Of course, this is part of a large profile hash that, prior to my attempt to use multiple constraints, was working fine. Now, here is the error I get:

[Thu Jan 31 15:58:27 2013] [error] [client] Value for constraint_method 'ccexp' not a code reference or Regexp . Do you need func(), not 'func'? at C:/Perl64/site/lib/Data/ line 974., referer: http://localhost:9080/test.dfv.html

I am using the following packages.

use Business::CreditCard; use Business::CCCheck; use Data::FormValidator; use Data::FormValidator::Constraints qw(:closures); use Data::FormValidator::Constraints::DateTime qw(:all);

If there are others that you believe would be useful, I'd appreciate hearing about them, but as far as I can see, the ones I am using are pretty much sufficient for what I need right now.

Any insight on how I can fix these constraints would be appreciated.



Replies are listed 'Best First'.
Re: Problems with not so trivial data validation using Data::FormValidator
by roboticus (Chancellor) on Feb 01, 2013 at 00:35 UTC


    I've never used Data::FormValidator, but looking at your code and the error message leads me to think that your second constraint should be more like:

    { constraint=>sub {after_today('%m%y')}, name => 'card_expired' }

    I came to that conclusion because after_today('%m%y') by itself isn't a code reference, but a function call. (Of course if after_today returned a code reference, then my suggestion is worth a bucket of mud.)

    Also for block quotes, I tend to use <blockquote><i> ... </i></blockquote>.


    When your only tool is a hammer, all problems look like your thumb.


      I gave that a try, but alas, it didn't change anything. I still get the same error.

      Thanks anyway.



        Have you looked at the tests for the module? You can use the test "t/multiple_constraints.t" as an example and build on it to find out how to use multiple constraints.


        When your only tool is a hammer, all problems look like your thumb.

Re: Problems with not so trivial data validation using Data::FormValidator (sscce, rt)
by Anonymous Monk on Feb 01, 2013 at 09:10 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1016409]
Approved by sundialsvc4
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-05-27 21:43 GMT
Find Nodes?
    Voting Booth?