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