Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How do I code multiple failure modes for Data::FormValidator field validations?

by trwww (Priest)
on Nov 15, 2010 at 23:37 UTC ( [id://871607]=note: print w/replies, xml ) Need Help??


in reply to How do I code multiple failure modes for Data::FormValidator field validations?

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

Replies are listed 'Best First'.
Re^2: How do I code multiple failure modes for Data::FormValidator field validations?
by markjugg (Curate) on Dec 15, 2010 at 01:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://871607]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found