Debian Codegraph @

t/compound/basic.t

raw
1 use Test::More;
2
3 use lib 't/lib';
4
5 use_ok( 'HTML::FormHandler::Field::Duration');
6 use HTML::FormHandler::I18N;
7 $ENV{LANGUAGE_HANDLE} = 'en_en';
8
9 my $field = HTML::FormHandler::Field::Duration->new( name => 'duration' );
10 $field->build_result;
11
12 ok( $field, 'get compound field');
13
14 my $input = {
15 hours => 1,
16 minutes => 2,
17 };
18
19 $field->_set_input($input);
20
21 is_deeply( $field->input, $input, 'field input is correct');
22
23 is_deeply( $field->fif, $input, 'field fif is same');
24
25 {
26 package Duration::Form;
27 use HTML::FormHandler::Moose;
28 extends 'HTML::FormHandler';
29
30 has_field 'name' => ( type => 'Text' );
31 has_field 'duration' => ( type => 'Duration' );
32 has_field 'duration.hours' => ( type => 'Nested' );
33 has_field 'duration.minutes' => ( type => 'Nested' );
34
35 }
36
37 my $form = Duration::Form->new;
38 ok( $form, 'get compound form' );
39 ok( $form->field('duration'), 'duration field' );
40 ok( $form->field('duration.hours'), 'duration.hours field' );
41
42 my $params = { name => 'Testing', 'duration.hours' => 2, 'duration.minutes' => 30 };
43
44 $form->process( params => $params );
45 ok( $form->validated, 'form validated' );
46
47 is_deeply($form->fif, $params, 'get fif with right value');
48 is( $form->field('duration')->value->hours, 2, 'duration value is correct');
49 $form->process( params => { name => 'Testing', 'duration.hours' => 'abc', 'duration.minutes' => 'xyz' } );
50 ok( $form->has_errors, 'form does not validate' );
51 my @errors = $form->errors;
52 is( $errors[0], 'Invalid value for Duration: Hours', 'correct error message' );
53
54 {
55 package Form::Start;
56 use HTML::FormHandler::Moose;
57 extends 'HTML::FormHandler';
58
59 has_field 'name' => ( type => 'Text' );
60 has_field 'start_date' => ( type => 'DateTime' );
61 has_field 'start_date.month' => ( type => 'Month' );
62 has_field 'start_date.day' => ( type => 'MonthDay' );
63 has_field 'start_date.year' => ( type => 'Year' );
64
65 sub validate_start_date_month
66 {
67 my ( $self, $field ) = @_;
68 $field->add_error("That month is not available")
69 if( $field->value == 8 );
70 }
71
72 }
73
74 my $dtform = Form::Start->new;
75 ok( $dtform, 'datetime form' );
76 my $year = (localtime)[5] + 1900;
77 $params = { name => 'DT_testing', 'start_date.month' => '10',
78 'start_date.day' => '2', 'start_date.year' => $year };
79 $dtform->process( params => $params );
80 ok( $dtform->validated, 'form validated' );
81 is( $dtform->field('start_date')->value->mdy, "10-02-$year", 'datetime value');
82 $params->{'start_date.month'} = 8;
83 $dtform->process( params => $params );
84 ok( !$dtform->validated, 'form did not validate' );
85 ok( $dtform->has_errors, 'form has error' );
86 @errors = $dtform->errors;
87 is_deeply( $errors[0], 'That month is not available', 'correct error' );
88
89 {
90 package Field::MyCompound;
91 use HTML::FormHandler::Moose;
92 extends 'HTML::FormHandler::Field::Compound';
93
94 has_field 'aaa';
95 has_field 'bbb';
96 }
97
98
99 {
100 package Form::TestValues;
101 use HTML::FormHandler::Moose;
102 extends 'HTML::FormHandler';
103
104 has_field 'compound' => ( type => '+Field::MyCompound', apply => [ { check => sub { $_[0]->{aaa} eq 'aaa'}, message => 'Must be "aaa"' } ] );
105 }
106 $form = Form::TestValues->new;
107 ok( $form, 'Compound form with separate fields declarations created' );
108
109 $params = {
110 'compound.aaa' => 'aaa',
111 'compound.bbb' => 'bbb',
112 };
113 $form->process( params => $params );
114 is_deeply( $form->values, { compound => { aaa => 'aaa', bbb => 'bbb' } }, 'Compound with separate fields - values in hash' );
115 is_deeply( $form->fif, $params, 'get fif from compound field' );
116 $form->process( params => { 'compound.aaa' => undef } );
117 ok( !$form->field( 'compound' )->has_errors, 'Not required compound with empty sub values is not checked');
118
119 {
120
121 package Compound;
122 use HTML::FormHandler::Moose;
123 extends 'HTML::FormHandler::Field::Compound';
124
125 has_field 'year' => (
126 type => 'Integer',
127 required => 1,
128 );
129
130 has_field 'month' => (
131 type => 'Integer',
132 range_start => 1,
133 range_end => 12,
134 );
135
136 has_field 'day' => (
137 type => 'Integer',
138 range_start => 1,
139 range_end => 31,
140 );
141
142 sub default {
143 return {
144 year => undef,
145 month => undef,
146 day => undef
147 };
148 }
149 }
150
151 {
152
153 package Form;
154 use HTML::FormHandler::Moose;
155 extends 'HTML::FormHandler';
156 has_field 'date' => ( type => '+Compound', required => 1 );
157 has_field 'foo';
158 }
159
160 my $f = Form->new;
161 $f->process( { 'date.day' => '18', 'date.month' => '2', 'date.year' => '2010' } );
162 is_deeply( $f->field('date')->value, { year => 2010, month => 2, day => 18 }, 'correct value' );
163
164 $f = Form->new;
165 $f->process( { foo => 'testing' } );
166 is_deeply( $f->field('date')->value, { year => undef, month => undef, day => undef }, 'correct default' );
167
168 done_testing;