Debian Codegraph @

t/fields/select.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use HTML::FormHandler::Field::Text;
5
6
7 {
8 package Test::Form;
9 use HTML::FormHandler::Moose;
10 extends 'HTML::FormHandler';
11
12 has '+name' => ( default => 'options_form' );
13 has_field 'test_field' => (
14 type => 'Text',
15 label => 'TEST',
16 id => 'f99',
17 );
18 has_field 'fruit' => ( type => 'Select' );
19 has_field 'vegetables' => ( type => 'Multiple' );
20 has_field 'empty' => ( type => 'Multiple', no_option_validation => 1 );
21 has_field 'build_attr' => ( type => 'Select' );
22
23 sub default_fruit { 2 }
24
25 # the following sometimes happens with db options
26 sub options_empty { ([]) }
27
28 has 'options_fruit' => ( is => 'rw', traits => ['Array'],
29 default => sub { [1 => 'apples', 2 => 'oranges',
30 3 => 'kiwi'] } );
31
32 sub options_vegetables {
33 return (
34 1 => 'lettuce',
35 2 => 'broccoli',
36 3 => 'carrots',
37 4 => 'peas',
38 );
39 }
40
41 has 'options_build_attr' => ( is => 'ro', traits => ['Array'], lazy_build => 1 );
42
43 sub _build_options_build_attr {
44 return [
45 1 => 'testing',
46 2 => 'moose',
47 3 => 'attr builder',
48 ];
49 }
50 }
51
52
53 my $form = Test::Form->new;
54 ok( $form, 'create form');
55
56 my $veg_options = [ {'label' => 'lettuce',
57 'value' => 1 },
58 {'label' => 'broccoli',
59 'value' => 2 },
60 {'label' => 'carrots',
61 'value' => 3 },
62 {'label' => 'peas',
63 'value' => 4 } ];
64 my $field_options = $form->field('vegetables')->options_ref;
65 is_deeply( $field_options, $veg_options,
66 'get options for vegetables' );
67
68 my $fruit_options = [ {'label' => 'apples',
69 'value' => 1 },
70 {'label' => 'oranges',
71 'value' => 2 },
72 {'label' => 'kiwi',
73 'value' => 3 } ];
74 $field_options = $form->field('fruit')->options;
75 is_deeply( $field_options, $fruit_options,
76 'get options for fruit' );
77
78 my $build_attr_options = [ {'label' => 'testing',
79 'value' => 1 },
80 {'label' => 'moose',
81 'value' => 2 },
82 {'label' => 'attr builder',
83 'value' => 3 } ];
84 $field_options = $form->field('build_attr')->options_ref;
85 is_deeply( $field_options, $build_attr_options,
86 'get options for fruit' );
87
88 is( $form->field('fruit')->value, 2, 'initial value ok');
89
90 $form->process( params => {},
91 init_object => { vegetables => undef, fruit => undef, build_attr => undef } );
92 $field_options = $form->field('vegetables')->options;
93 is_deeply( $field_options, $veg_options,
94 'get options for vegetables after process' );
95 $field_options = $form->field('fruit')->options;
96 is_deeply( $field_options, $fruit_options,
97 'get options for fruit after process' );
98 $field_options = $form->field('build_attr')->options;
99 is_deeply( $field_options, $build_attr_options,
100 'get options for fruit after process' );
101
102 my $params = {
103 fruit => 2,
104 vegetables => [2,4],
105 empty => 'test',
106 };
107
108 $form->process( $params );
109 ok( $form->validated, 'form validated' );
110 is( $form->field('fruit')->value, 2, 'fruit value is correct');
111 is_deeply( $form->field('vegetables')->value, [2,4], 'vegetables value is correct');
112
113 is_deeply( $form->fif, { fruit => 2, vegetables => [2, 4], empty => ['test'], test_field => '', build_attr => '' },
114 'fif is correct');
115 is_deeply( $form->values, { fruit => 2, vegetables => [2, 4], empty => ['test'], build_attr => undef },
116 'values are correct');
117 is( $form->field('vegetables')->as_label, 'broccoli, peas', 'multiple as_label works');
118 is( $form->field('vegetables')->as_label([3,4]), 'carrots, peas', 'pass in multiple as_label works');
119
120 $params = {
121 fruit => 2,
122 vegetables => 4,
123 };
124 $form->process($params);
125 is_deeply( $form->field('vegetables')->value, [4], 'value for vegetables correct' );
126 is_deeply( $form->field('vegetables')->fif, [4], 'fif for vegetables correct' );
127
128 {
129 package Test::Form2;
130 use HTML::FormHandler::Moose;
131 extends 'HTML::FormHandler';
132
133 has_field 'my_list' => ( type => 'Select' );
134
135 # this adds a 'selected' hash key to an option as an alternative
136 # to setting the default for the field.
137 sub options_my_list {
138 return [
139 {
140 value => 1,
141 label => 'One',
142 selected => 1,
143 },
144 {
145 value => 2,
146 label => 'Two',
147 },
148 {
149 value => 3,
150 label => 'Three',
151 }
152 ];
153 }
154
155 }
156
157 $form = Test::Form2->new;
158 ok( $form, 'form built' );
159
160 my $rendered_field = $form->field('my_list')->render;
161 like( $rendered_field, qr/<option value="1" id="my_list\.0" selected="selected">/, 'element is selected' );
162 # the 'value' of the field should reflect the selected values
163 is_deeply( $form->value, { my_list => 1 }, 'value ok' );
164 ok( $form->field('my_list')->fif, 'fif value');
165 $form->process( { my_list => 2 } );
166 is_deeply( $form->fif, { my_list => 2 }, 'fif is correct' );
167 $rendered_field = $form->field('my_list')->render;
168 like( $rendered_field, qr/<option value="2" id="my_list\.1" selected="selected">/, 'element is selected' );
169
170
171 {
172 package Test::Form3;
173 use HTML::FormHandler::Moose;
174 extends 'HTML::FormHandler';
175
176 has_field fruit => (
177 type => 'Select',
178 options => [ [ qw(apricot banana cherry) ] ],
179 );
180 }
181
182 $form = Test::Form3->new;
183 ok( $form, 'form 3 built' );
184 my $expected = [ map { label => $_, value => $_ }, qw(apricot banana cherry) ];
185 is_deeply [ $form->field('fruit')->options ], $expected,
186 'Form 3 has expected options';
187 $rendered_field = $form->field('fruit')->render;
188 like $rendered_field, qr/<option value="cherry"/, 'rendered a field';
189
190 my @day_options = qw(Monday Tuesday Wednesday);
191 {
192 package Test::Form4;
193 use HTML::FormHandler::Moose;
194 extends 'HTML::FormHandler';
195
196 has_field day => ( type => 'Select' );
197 sub options_day { return [[ @day_options ]] }
198 }
199
200 $form = Test::Form4->new;
201 ok( $form, 'form 4 built' );
202 my @expected_days = map { label => $_, value => $_ }, @day_options;
203 is_deeply [ $form->field('day')->options ], \@expected_days,
204 'Form 4 has expected options';
205 $rendered_field = $form->field('day')->render;
206 like $rendered_field, qr/<option value="Tuesday"/, 'rendered a field';
207
208 # following test is for 'has_many' select field flag
209 {
210 package Test::HasMany;
211 use HTML::FormHandler::Moose;
212 extends 'HTML::FormHandler';
213
214 has_field 'foo' => ( default => 'my_foo' );
215 has_field 'hm_bar' => ( type => 'Multiple',
216 has_many => 'my_id', default => [3] );
217
218 sub options_hm_bar { [1, 2, 3, 4] }
219 }
220 $form = Test::HasMany->new;
221 ok( $form, 'has many form built' );
222 $form->process( params => {} );
223 my $fif_expected = { foo => 'my_foo', hm_bar => [3] };
224 is_deeply( $form->fif, $fif_expected, 'got expected fif' );
225 $form->process( params => { foo => 'my_foo', hm_bar => [4] } );
226 my $val_expected = { foo => 'my_foo', hm_bar => [ { my_id => 4 } ] };
227 is_deeply( $form->value, $val_expected, 'got expected value' );
228 $fif_expected = { foo => 'my_foo', hm_bar => [4] };
229 is_deeply( $form->fif, $fif_expected, 'got expected fif' );
230 $form->process( params => { foo => 'my_foo', hm_bar => [1,2] } );
231 $fif_expected = { foo => 'my_foo', hm_bar => [1,2] };
232 is_deeply( $form->fif, $fif_expected, 'got expected fif again' );
233 $val_expected = { foo => 'my_foo', hm_bar => [ { my_id => 1 }, { my_id => 2 } ] };
234 is_deeply( $form->value, $val_expected, 'got expected value again' );
235
236 {
237 package Test::Multiple::InitObject;
238 use HTML::FormHandler::Moose;
239 extends 'HTML::FormHandler';
240
241 has_field 'foo' => ( default => 'my_foo' );
242 has_field 'bar' => ( type => 'Multiple' );
243
244 sub options_bar {
245 return (
246 1 => 'one',
247 2 => 'two',
248 3 => 'three',
249 4 => 'four',
250 );
251 }
252
253
254 }
255
256 $form = Test::Multiple::InitObject->new;
257 my $init_object = { foo => 'new_foo', bar => [3,4] };
258 $form->process(init_object => $init_object, params => {} );
259 my $rendered = $form->render;
260 like($rendered, qr/<option value="4" id="bar.1" selected="selected">four<\/option>/, 'rendered option');
261 my $value = $form->value;
262 is_deeply( $value, $init_object, 'correct value');
263
264 done_testing;