Debian Codegraph @

t/render/simple.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use HTML::FormHandler::Test;
6 use HTML::FormHandler::Field::Text;
7
8
9 {
10 package Test::Form;
11 use HTML::FormHandler::Moose;
12 extends 'HTML::FormHandler';
13 with 'HTML::FormHandler::Render::Simple';
14
15 sub build_do_form_wrapper { 1 }
16 sub build_form_wrapper_class { 'form_wrapper' }
17 has '+name' => ( default => 'testform' );
18 has_field 'test_field' => (
19 element_class => 'test123',
20 size => 20,
21 label => 'TEST',
22 id => 'f99',
23 );
24 has_field 'number';
25 has_field 'fruit' => ( type => 'Select' );
26 has_field 'cheese' => ( type => 'Select' );
27 has_field 'vegetables' => ( type => 'Multiple' );
28 has_field 'grains' => ( type => 'Multiple' );
29 has_field 'opt_in' => ( type => 'Select', widget => 'RadioGroup',
30 options => [{ value => 0, label => 'No'}, { value => 1, label => 'Yes'} ] );
31 has_field 'starch' => ( type => 'Multiple', widget => 'CheckboxGroup',
32 options => [{ value => 1, label => 'One'}, { value => 2, label => 'Two'},
33 { value => 3, label => 'Three' },
34 ] );
35 has_field 'active' => ( type => 'Checkbox' );
36 has_field 'comments' => ( type => 'TextArea', cols => 40, rows => 3 );
37 has_field 'hidden' => ( type => 'Hidden' );
38 has_field 'selected' => ( type => 'Boolean' );
39 has_field 'start_date' => ( type => 'DateTime', do_wrapper => 1,
40 tags => { wrapper_tag => 'fieldset' },
41 wrapper_attr => { class => 'start_date' },
42 );
43 has_field 'start_date.month' => ( type => 'Integer', range_start => 1,
44 range_end => 12 );
45 has_field 'start_date.day' => ( type => 'Integer', range_start => 1,
46 range_end => 31 );
47 has_field 'start_date.year' => ( type => 'Integer', range_start => 2000,
48 range_end => 2020 );
49
50 has_field 'two_errors' => (
51 apply => [
52 { check => [ ], message => 'First constraint error' },
53 { check => [ ], message => 'Second constraint error' }
54 ]
55 );
56
57 has_field 'submit' => ( type => 'Submit', value => 'Update' );
58
59 has '+dependency' => ( default => sub { [ ['start_date.month',
60 'start_date.day', 'start_date.year'] ] } );
61 has_field 'no_render' => ( widget => 'NoRender' );
62 sub options_fruit {
63 return (
64 1 => 'apples',
65 2 => 'oranges',
66 3 => 'kiwi',
67 );
68 }
69
70 sub options_vegetables {
71 return (
72 1 => 'lettuce',
73 2 => 'broccoli',
74 3 => 'carrots',
75 4 => 'peas',
76 );
77 }
78
79 sub options_grains {
80 return [
81 { value => 1, label => 'maize', disabled => 0 },
82 { value => 2, label => 'rice', disabled => 1 },
83 { value => 3, label => 'wheat' },
84 ];
85 }
86
87 sub options_cheese {
88 return [
89 { value => 1, label => 'canastra', disabled => 0 },
90 { value => 2, label => 'brie', disabled => 1 },
91 { value => 3, label => 'gorgonzola' },
92 ];
93 }
94
95 sub html_attributes {
96 my ( $self, $field, $type, $attr ) = @_;
97 $attr->{class} = 'label' if $type eq 'label';
98 return $attr;
99 }
100 }
101
102
103 my $form = Test::Form->new;
104 ok( $form, 'create form');
105
106 my $params = {
107 test_field => 'something',
108 number => 0,
109 fruit => 2,
110 vegetables => [2,4],
111 active => 'now',
112 comments => 'Four score and seven years ago...',
113 hidden => '1234',
114 selected => '1',
115 'start_date.month' => '7',
116 'start_date.day' => '14',
117 'start_date.year' => '2006',
118 two_errors => 'aaa',
119 opt_in => 0,
120 };
121
122 $form->process( $params );
123
124 is_deeply( $form->field('starch')->input_without_param, [], 'checkbox group settings' );
125 is( $form->field('starch')->not_nullable, 1, 'checkbox group settings' );
126 is_deeply( $form->value->{starch}, [], 'checkbox group value' );
127
128 is_html( $form->render_field( $form->field('number') ),
129 '
130 <div><label class="label" for="number">Number</label><input type="text" name="number" id="number" value="0" />
131 </div>',
132 "value '0' is rendered"
133 );
134
135 my $output1 = $form->render_field( $form->field('test_field') );
136 is_html( $output1,
137 '
138 <div><label class="label" for="f99">TEST</label><input type="text" name="test_field" id="f99" size="20" value="something" class="test123" />
139 </div>',
140 'output from text field');
141
142 my $output2 = $form->render_field( $form->field('fruit') );
143 is_html( $output2,
144 '
145 <div><label class="label" for="fruit">Fruit</label><select name="fruit" id="fruit"><option value="1" id="fruit.0">apples</option><option value="2" id="fruit.1" selected="selected">oranges</option><option value="3" id="fruit.2">kiwi</option></select>
146 </div>',
147 'output from select field');
148
149 my $output12 = $form->render_field( $form->field('cheese') );
150 is_html( $output12,
151 '
152 <div><label class="label" for="cheese">Cheese</label><select name="cheese" id="cheese"><option value="1" id="cheese.0">canastra</option><option value="2" id="cheese.1" disabled="disabled">brie</option><option value="3" id="cheese.2">gorgonzola</option></select>
153 </div>',
154 'output from select field with disabled option');
155
156 my $output3 = $form->render_field( $form->field('vegetables') );
157 is_html( $output3,
158 '
159 <div><label class="label" for="vegetables">Vegetables</label><select name="vegetables" id="vegetables" multiple="multiple" size="5"><option value="1" id="vegetables.0">lettuce</option><option value="2" id="vegetables.1" selected="selected">broccoli</option><option value="3" id="vegetables.2">carrots</option><option value="4" id="vegetables.3" selected="selected">peas</option></select>
160 </div>',
161 'output from select multiple field');
162
163 my $output13 = $form->render_field( $form->field('grains') );
164 is_html( $output13,
165 '
166 <div><label class="label" for="grains">Grains</label><select name="grains" id="grains" multiple="multiple" size="5"><option value="1" id="grains.0">maize</option><option value="2" id="grains.1" disabled="disabled">rice</option><option value="3" id="grains.2">wheat</option></select>
167 </div>',
168 'output from select multiple field with disabled option');
169
170 my $output4 = $form->render_field( $form->field('active') );
171 is_html( $output4,
172 '
173 <div><label class="label" for="active">Active</label><input type="checkbox" name="active" id="active" value="1" />
174 </div>',
175 'output from checkbox field');
176
177 my $output5 = $form->render_field( $form->field('comments') );
178 is_html( $output5,
179 '
180 <div><label class="label" for="comments">Comments</label><textarea name="comments" id="comments" rows="3" cols="40">Four score and seven years ago...</textarea>
181 </div>',
182 'output from textarea' );
183
184 my $output6 = $form->render_field( $form->field('hidden') );
185 is_html( $output6,
186 '<div><input type="hidden" name="hidden" id="hidden" value="1234" /></div>',
187 'output from hidden field' );
188
189 my $output7 = $form->render_field( $form->field('selected') );
190 is_html( $output7,
191 '
192 <div><label class="label" for="selected">Selected</label><input type="checkbox" name="selected" id="selected" value="1" checked="checked" />
193 </div>',
194 'output from boolean' );
195
196 my $output8 = $form->render_field( $form->field('start_date') );
197 is_html( $output8,
198 '
199 <fieldset class="start_date" id="start_date"><legend>Start date</legend>
200 <div><label class="label" for="start_date.month">Month</label><input type="text" name="start_date.month" id="start_date.month" size="8" value="7" />
201 </div>
202 <div><label class="label" for="start_date.day">Day</label><input type="text" name="start_date.day" id="start_date.day" size="8" value="14" />
203 </div>
204 <div><label class="label" for="start_date.year">Year</label><input type="text" name="start_date.year" id="start_date.year" size="8" value="2006" />
205 </div>
206 </fieldset>',
207 'output from DateTime' );
208
209 my $output9 = $form->render_field( $form->field('submit') );
210 is_html( $output9, '
211 <div><input type="submit" name="submit" id="submit" value="Update" />
212 </div>', 'output from Submit');
213
214 my $output10 = $form->render_field( $form->field('opt_in') );
215 is_html( $output10, '
216 <div><label class="label" for="opt_in">Opt in</label> <br /><label for="opt_in.0"><input type="radio" value="0" name="opt_in" id="opt_in.0" checked="checked" />No</label><br /><label for="opt_in.1"><input type="radio" value="1" name="opt_in" id="opt_in.1" />Yes</label><br />
217 </div>', 'output from radio group' );
218
219 my $output11 = $form->render_start;
220 is_html( $output11,
221 '<form id="testform" method="post"><fieldset class="form_wrapper">',
222 'Form start OK' );
223
224 my $output = $form->render;
225 ok( $output, 'get rendered output from form');
226
227 is_html( $form->render_field( $form->field('no_render')), '', 'no_render' );
228
229 done_testing;