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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This is probably the profile I'd use for the requirements:

our $profile = { filters => ['trim'], required => [ 'Field1', 'Field2', ], constraint_methods => { 'Field1' => [ { name => 'not_positive', constraint_method => sub { my ($dfv, $val) = @_; return $val =~ /\A\d+\z/, } }, ], 'Field2' => [ { name => 'not_positive', constraint_method => sub { my ($dfv, $val) = @_; return $val =~ /\A\d+\z/, }, }, { name => 'field2_too_big', params => [ qw(Field1 Field2) ], constraint_method => sub { my ($dfv, $f1, $f2) = @_; no warnings qw(uninitialized); return $f2 < $f1; }, }, ], }, msgs => { invalid_seperator => ' ## ', format => 'ERROR: %s', missing => 'FIELD IS REQUIRED', invalid => 'FIELD IS INVALID', prefix => 'err_', constraints => { not_positive => 'MUST BE POSITIVE', field2_too_big => 'MUST BE < Field1', } }, };

Observe:

use warnings; use strict; use Data::FormValidator; use Test::More tests => 15; our $profile = { filters => ['trim'], required => [ 'Field1', 'Field2', ], constraint_methods => { 'Field1' => [ { name => 'not_positive', constraint_method => sub { my ($dfv, $val) = @_; return $val =~ /\A\d+\z/, } }, ], 'Field2' => [ { name => 'not_positive', constraint_method => sub { my ($dfv, $val) = @_; return $val =~ /\A\d+\z/, }, }, { name => 'field2_too_big', params => [ qw(Field1 Field2) ], constraint_method => sub { my ($dfv, $f1, $f2) = @_; no warnings qw(uninitialized); return $f2 < $f1; }, }, ], }, msgs => { invalid_seperator => ' ## ', format => 'ERROR: %s', missing => 'FIELD IS REQUIRED', invalid => 'FIELD IS INVALID', prefix => 'err_', constraints => { not_positive => 'MUST BE POSITIVE', field2_too_big => 'MUST BE < Field1', } }, }; my($results, %msgs); isa_ok( $results = Data::FormValidator->check( {}, $profile ) => 'Data::FormValidator::Results' ); ok(! $results->success, 'data is invalid'); %msgs = %{$results->msgs}; while( my($k, $v) = each(%msgs) ) { is($v => 'ERROR: FIELD IS REQUIRED', "$k is required") } isa_ok( $results = Data::FormValidator->check( { Field1 => -10 }, $profile ) => 'Data::FormValidator::Results' ); ok(! $results->success, 'data is invalid'); is( $results->msgs->{err_Field1} => 'ERROR: MUST BE POSITIVE', 'Field1 must be positive' ); isa_ok( $results = Data::FormValidator->check( { Field2 => -10 }, $profile ) => 'Data::FormValidator::Results' ); ok(! $results->success, 'data is invalid'); is( $results->msgs->{err_Field2} => 'ERROR: MUST BE POSITIVE', 'Field2 must be positive' ); isa_ok( $results = Data::FormValidator->check( { Field1 => 5, Field2 => 20 }, $profile ) => 'Data::FormValidator::Results' ); ok(! $results->success, 'data is invalid'); is( $results->msgs->{err_Field2} => 'ERROR: MUST BE < Field1', 'Field2 must less than Field1' ); isa_ok( $results = Data::FormValidator->check( { Field1 => 20, Field2 => 5 }, $profile ) => 'Data::FormValidator::Results' ); ok($results->success, 'data is valid');

Output:

$ prove -v test.pl test.pl .. 1..15 ok 1 - The object isa Data::FormValidator::Results ok 2 - data is invalid ok 3 - err_Field1 is required ok 4 - err_Field2 is required ok 5 - The object isa Data::FormValidator::Results ok 6 - data is invalid ok 7 - Field1 must be positive ok 8 - The object isa Data::FormValidator::Results ok 9 - data is invalid ok 10 - Field2 must be positive ok 11 - The object isa Data::FormValidator::Results ok 12 - data is invalid ok 13 - Field2 must less than Field1 ok 14 - The object isa Data::FormValidator::Results ok 15 - data is valid ok All tests successful. Files=1, Tests=15, 1 wallclock secs ( 0.03 usr 0.01 sys + 0.04 cusr + 0.00 csys = 0.08 CPU) Result: PASS

In reply to Re: How do I code multiple failure modes for Data::FormValidator field validations? by trwww
in thread How do I code multiple failure modes for Data::FormValidator field validations? by thezip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (17)
    As of 2015-07-31 15:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (278 votes), past polls