use Test::More tests => 11;
use strict;
use Data::Dumper;
use Data::FormValidator;
#
# Prove this statement in the POD:
# The exception here is definitions for your msgs key. You will safely be able to define
# some defaults for this key and not have them entirely clobbered just because msgs was
# defined in a validation profile.
#
eval {
my $defaults = {
msgs => {
constraints => {
default1 => 'default message',
override1 => 'default message',
},
},
};
my $data = {
profile1 => 1,
inline1 => 1,
override1 => 1,
};
my $profile = {
required_regexp => qr/./,
constraints => {
profile1 => {
name => 'profile1',
constraint => sub {return},
},
},
constraint_methods => {
inline1 => cl_override_msg('inline1', 'inline message'),
override1 => cl_override_msg('override1','profile message'),
},
msgs => {
constraints => {
profile1 => 'profile message',
override1 => 'profile message',
},
},
};
my $validator = Data::FormValidator->new($defaults);
ok($validator,'validator instance created');
eval{
my $results = $validator->check($data, $profile);
my $msgs = $results->msgs;
ok($msgs,'messages exists. line ' . __LINE__);
like($msgs->{profile1}, qr/profile message/,'profile1 uses profile message. line ' . __LINE__);
like($msgs->{inline1}, qr/inline message/, 'inline1 uses inline message. line ' . __LINE__);
like($msgs->{override1}, qr/profile message/,'override1 uses profile message. line ' . __LINE__);
};
is ($@, '', "No exception at line " . __LINE__);
};
is ($@, '', "No exception at line " . __LINE__);
#
# Prove that constraint messages can be overridden using the msgs() method from within the constraint.
#
eval {
sub cl_override_msg {
my ($name,$message) = @_;
return sub {
my $dfv = shift;
$dfv->name_this($name);
my $mhash = {};
$mhash->{constraints}{$name} = $message;
$dfv->msgs( $mhash );
return;
};
}
my $results = Data::FormValidator->check(
{
profile1 => 1,
inline1 => 1,
override1 => 1,
},
{
required => [qw/profile1 inline1 override1/],
constraints => => {
profile1 => {
name => 'profile1',
constraint => sub {return},
},
},
constraint_methods => {
inline1 => cl_override_msg('inline1', 'inline message'),
override1 => cl_override_msg('override1','profile message'),
},
msgs => {
constraints => {
profile1 => 'profile message',
override1 => 'profile message',
},
},
}
);
my $msgs = $results->msgs;
like($msgs->{profile1},qr/profile message/,'profile1 uses profile message. line ' . __LINE__);
like($msgs->{inline1}, qr/inline message/,'inline1 uses inline message. line ' . __LINE__);
like($msgs->{override1}, qr/profile message/,'override1 uses profile message. line ' . __LINE__);
};
is ($@, '', "No exception at line " . __LINE__);