| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use lib 't/lib'; |
| 5 |
|
| 6 |
{ |
| 7 |
package My::Constraints; |
| 8 |
|
| 9 |
sub check_something { |
| 10 |
my $value = shift; |
| 11 |
return 1 if $value eq 'something'; |
| 12 |
return 0; |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
{ |
| 17 |
|
| 18 |
package My::Form; |
| 19 |
use HTML::FormHandler::Moose; |
| 20 |
extends 'HTML::FormHandler'; |
| 21 |
|
| 22 |
use Moose::Util::TypeConstraints; |
| 23 |
|
| 24 |
subtype 'Natural' |
| 25 |
=> as 'Int' |
| 26 |
=> where { $_ > 0 }; |
| 27 |
|
| 28 |
subtype 'NaturalLessThanTen' |
| 29 |
=> as 'Natural' |
| 30 |
=> where { $_ < 10 } |
| 31 |
=> message { "This number ($_) is not less than ten!" }; |
| 32 |
|
| 33 |
coerce 'Num' |
| 34 |
=> from 'Str' |
| 35 |
=> via { 0+$_ }; |
| 36 |
|
| 37 |
enum 'RGBColors' => [ qw(red green blue) ]; |
| 38 |
|
| 39 |
no Moose::Util::TypeConstraints; |
| 40 |
|
| 41 |
has_field 'message_arrayref' => ( |
| 42 |
apply => [ { check => qr/aaa/, message => ['Must contain [_1]', 'aaa'] } ], |
| 43 |
); |
| 44 |
has_field 'regex_error' => ( |
| 45 |
apply => [ { check => qr/xyz/ } ], |
| 46 |
); |
| 47 |
has_field 'regex_correct' => ( |
| 48 |
apply => [ { check => qr/aaa/, message => 'Must contain aaa' } ], |
| 49 |
); |
| 50 |
has_field 'message_sub' => ( |
| 51 |
apply => [ |
| 52 |
{ |
| 53 |
check => [ 'abc' ], |
| 54 |
message => \&err_message |
| 55 |
} |
| 56 |
] |
| 57 |
); |
| 58 |
sub err_message { |
| 59 |
my ($value, $field ) = @_; |
| 60 |
return $field->name . ': Must be "abc"'; |
| 61 |
} |
| 62 |
|
| 63 |
has_field 'set_error' => ( |
| 64 |
apply => [ |
| 65 |
{ |
| 66 |
check => [ 'abc', 'bbb' ], |
| 67 |
message => 'Must be "aaa" or "bbb"' |
| 68 |
} |
| 69 |
] |
| 70 |
); |
| 71 |
has_field 'set_correct' => ( |
| 72 |
apply => [ |
| 73 |
{ |
| 74 |
check => [ 'aaa', 'bbb' ], |
| 75 |
message => 'Must be "aaa" or "bbb"' |
| 76 |
} |
| 77 |
] |
| 78 |
); |
| 79 |
has_field 'callback_error' => ( |
| 80 |
apply => [ |
| 81 |
{ |
| 82 |
check => sub { if ( $_[0] =~ /(\d+)/ ) { return $1 > 10 } }, |
| 83 |
message => 'Must contain number greater than 10', |
| 84 |
} |
| 85 |
] |
| 86 |
); |
| 87 |
has_field 'callback_pass' => ( |
| 88 |
apply => [ |
| 89 |
{ |
| 90 |
check => \&check_callback_pass, |
| 91 |
message => 'Must contain number greater than 10', |
| 92 |
} |
| 93 |
] |
| 94 |
); |
| 95 |
sub check_callback_pass { |
| 96 |
my ( $value, $field ) = @_; |
| 97 |
if( $value =~ /(\d+)/ ) { |
| 98 |
return $1 > 10; |
| 99 |
} |
| 100 |
} |
| 101 |
has_field 'less_than_ten_error' => ( |
| 102 |
apply => [ 'NaturalLessThanTen' ] |
| 103 |
); |
| 104 |
has_field 'less_than_ten_pass' => ( |
| 105 |
apply => [ 'NaturalLessThanTen' ] |
| 106 |
); |
| 107 |
has_field 'my_something' => ( |
| 108 |
apply => [{ check => \&My::Constraints::check_something, |
| 109 |
message => 'Something is not right' }], |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
my $form = My::Form->new(); |
| 114 |
ok( $form, 'get form' ); |
| 115 |
|
| 116 |
my $params = { |
| 117 |
message_arrayref => 'xxx', |
| 118 |
regex_error => 'bbb', |
| 119 |
regex_correct => 'bbb aaa', |
| 120 |
set_error => 'ccc', |
| 121 |
set_correct => 'aaa', |
| 122 |
callback_error => 'asdf 2', |
| 123 |
callback_pass => 'asdf 20 asd', |
| 124 |
less_than_ten_error => 10, |
| 125 |
less_than_ten_pass => 9, |
| 126 |
message_sub => 'xyz', |
| 127 |
my_something => 'nothing', |
| 128 |
}; |
| 129 |
$form->process($params); |
| 130 |
ok( $form->field('message_arrayref')->has_errors, 'message arrayref works' ); |
| 131 |
ok( $form->field('regex_error')->has_errors, 'regexp constraint - error' ); |
| 132 |
ok( !$form->field('regex_correct')->has_errors, 'regexp constraint - pass' ); |
| 133 |
ok( $form->field('regex_correct')->has_value, 'constraints passed - has_value is true' ); |
| 134 |
is( $form->field('message_sub')->errors->[0], 'message_sub: Must be "abc"', 'error from message sub' ); |
| 135 |
ok( !$form->field('set_correct')->has_errors, 'set correct' ); |
| 136 |
ok( $form->field('set_error')->has_errors, 'set error' ); |
| 137 |
ok( $form->field('callback_error')->has_errors, 'callback constraint - error' ); |
| 138 |
ok( !$form->field('callback_pass')->has_errors, 'callback constraint - pass' ); |
| 139 |
ok( $form->field('less_than_ten_error')->has_errors, 'type constraint - error' ); |
| 140 |
my $message = $form->field('less_than_ten_error')->errors->[0]; |
| 141 |
is( $message, "This number (10) is not less than ten!", 'type constraint - error message' ); |
| 142 |
ok( !$form->field('less_than_ten_pass')->has_errors, 'type constraint - pass' ); |
| 143 |
is( $form->field('my_something')->errors->[0], 'Something is not right', 'check sub from package' ); |
| 144 |
#warn Dumper( $form ); use Data::Dumper; |
| 145 |
is_deeply( $form->fif, $params, 'fif is correct'); |
| 146 |
|
| 147 |
done_testing; |