| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
use Test::Exception; |
| 5 |
|
| 6 |
use HTML::FormHandler::Types (':all'); |
| 7 |
|
| 8 |
use HTML::FormHandler::I18N; |
| 9 |
$ENV{LANGUAGE_HANDLE} = 'en_en'; |
| 10 |
|
| 11 |
BEGIN { |
| 12 |
plan skip_all => 'Type::Tiny or Type::Tiny::Enum not installed' |
| 13 |
unless eval { require Type::Tiny; require Type::Tiny::Enum; }; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
{ |
| 18 |
package Test::Form::Type::Tiny; |
| 19 |
|
| 20 |
use HTML::FormHandler::Moose; |
| 21 |
extends 'HTML::FormHandler'; |
| 22 |
|
| 23 |
use Type::Tiny::Enum; |
| 24 |
my $ENUM = Type::Tiny::Enum->new( |
| 25 |
name => "Meta", |
| 26 |
values => [qw( foo bar )], |
| 27 |
message => sub { "$_ ain't meta" }, |
| 28 |
); |
| 29 |
|
| 30 |
my $NUM = Type::Tiny->new( |
| 31 |
name => "Number", |
| 32 |
constraint => sub { $_ =~ /^\d+$/ }, |
| 33 |
message => sub { "$_ ain't a number" }, |
| 34 |
); |
| 35 |
|
| 36 |
has_field 'test_a' => ( apply => [ $NUM ] ); |
| 37 |
has_field 'test_b' => ( apply => [ { type => $NUM } ] ); |
| 38 |
has_field 'test_c' => ( apply => [ $ENUM ] ); |
| 39 |
has_field 'test_d' => ( apply => [ { type => $ENUM } ] ); |
| 40 |
} |
| 41 |
|
| 42 |
my $form = Test::Form::Type::Tiny->new; |
| 43 |
|
| 44 |
ok($form, 'get form'); |
| 45 |
|
| 46 |
my $params = { |
| 47 |
test_a => 'str1', |
| 48 |
test_b => 'str2', |
| 49 |
test_c => 'str3', |
| 50 |
test_d => 'str4', |
| 51 |
}; |
| 52 |
$form->process($params); |
| 53 |
ok( !$form->validated, 'form did not validate' ); |
| 54 |
ok( $form->field('test_a')->has_errors, 'errors on Type::Tiny type'); |
| 55 |
ok( $form->field('test_b')->has_errors, 'errors on Type::Tiny type'); |
| 56 |
ok( $form->field('test_c')->has_errors, 'errors on Type::Tiny::Enum type'); |
| 57 |
ok( $form->field('test_d')->has_errors, 'errors on Type::Tiny::Enum type'); |
| 58 |
is( $form->field('test_a')->errors->[0], "str1 ain't a number", 'error from Type::Tiny' ); |
| 59 |
is( $form->field('test_b')->errors->[0], "str2 ain't a number", 'error from Type::Tiny' ); |
| 60 |
is( $form->field('test_c')->errors->[0], "str3 ain't meta", 'error from Type::Tiny::Enum' ); |
| 61 |
is( $form->field('test_d')->errors->[0], "str4 ain't meta", 'error from Type::Tiny::Enum' ); |
| 62 |
|
| 63 |
$params = { |
| 64 |
test_a => '123', |
| 65 |
test_b => '456', |
| 66 |
test_c => 'foo', |
| 67 |
test_d => 'bar', |
| 68 |
}; |
| 69 |
$form->process($params); |
| 70 |
ok( $form->validated, 'form validated' ); |
| 71 |
ok( !$form->field('test_a')->has_errors, 'no errors on Type::Tiny type'); |
| 72 |
ok( !$form->field('test_b')->has_errors, 'no errors on Type::Tiny type'); |
| 73 |
ok( !$form->field('test_c')->has_errors, 'no errors on Type::Tiny::Enum type'); |
| 74 |
ok( !$form->field('test_d')->has_errors, 'no errors on Type::Tiny::Enum type'); |
| 75 |
|
| 76 |
done_testing; |