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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?
    [perldigious]: Snowshoeing time stevieb? I need to buy a pair so I can walk my German Shepherd properly through the country.
    [stevieb]: yeah, I snowshoe a fair amount in the mountains. I also use spikes similar to these very often as well
    [perldigious]: Discipulus: alternate link
    [stevieb]: LOL you can easily replace "Helsinki" with "Alberta"
    [perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.
    [choroba]: Why don't they use tire chains?
    [perldigious]: First snow, probably not prepared right away choroba.
    [stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
    [perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
    [stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (10)
    As of 2016-12-06 16:34 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:













      Results (112 votes). Check out past polls.