10 use_ok('HTML::Form::Simple');
12 my $form = HTML::Form::Simple->new;
13 ok($form, 'new form');
24 $form->start({method => 'get', action => '', ignore => undef}),
25 '<form action="" method="get">',
26 'start with attributes'
30 eval { $form->start('should be a hashref') },
45 '<input type="hidden">',
50 $form->hidden(foo => 'bar'),
51 '<input name="foo" type="hidden" value="bar">',
56 $form->hidden(undef, undef, {value => 'bar', name => 'foo', id => 'foo'}),
57 '<input id="foo" name="foo" type="hidden" value="bar">',
62 [ $form->hidden(foo => [1, 0], {class => 'test'}) ],
64 '<input class="test" name="foo" type="hidden" value="1">',
65 '<input class="test" name="foo" type="hidden" value="0">',
71 [ $form->hidden({2 => 0, 10 => 1}, {class => 'test'}) ],
73 '<input class="test" name="10" type="hidden" value="1">',
74 '<input class="test" name="2" type="hidden" value="0">',
82 scalar $form->hidden({2 => [1, 0], 10 => 2}, {class => 'test'}),
84 '<input class="test" name="10" type="hidden" value="2">',
85 '<input class="test" name="2" type="hidden" value="1">',
86 '<input class="test" name="2" type="hidden" value="0">',
96 '<input type="submit">',
101 $form->submit('<OK>'),
102 '<input type="submit" value="<OK>">',
107 $form->submit({disabled => 1, id => 'test', type => 'button'}),
108 '<input disabled id="test" type="button">',
113 $form->submit('<OK>', {type => '', value => 'override', id => ''}),
114 '<input value="<OK>">',
122 '<input type="text">',
127 $form->text(undef, undef, undef),
128 '<input type="text">',
129 'explicit empty input'
134 '<input id="test" name="test" type="text">',
139 $form->text(undef, 'test'),
140 '<input type="text" value="test">',
145 $form->text(undef, {value => 'test'}),
146 '<input type="text" value="test">',
147 'input with attribute value'
151 $form->text({name => 'test', value => ''}),
152 '<input id="test" name="test" type="text" value="">',
153 'input with only attributes'
157 $form->text('', '', {
163 value => 'overrides',
165 '<input name="" type="text" value="">',
166 'input with empty attributes'
170 $form->text(undef, undef, {name => '0', value => '0'}),
171 '<input id="0" name="0" type="text" value="0">',
176 $form->text('name', {id => ''}),
177 '<input name="name" type="text">',
178 'input with id override'
182 $form->text('<">', '<">', {id => '>"<'}),
183 '<input id=">"<" name="<">" type="text" value="<">">',
188 $form->text(undef, {disabled => 'something'}),
189 '<input disabled type="text">',
194 $form->text({type => 'password'}),
195 '<input type="password">',
202 $form->text({rows => 0}),
203 '<textarea rows="0"></textarea>',
208 $form->text(foo => 'bar', {cols => 42, rows => 1, disabled => 1}),
209 '<textarea disabled cols="42" id="foo" name="foo" rows="1">bar</textarea>',
214 $form->text(undef, qq{<foo>&bl'a"\n .}, {cols => undef, rows => '<">'}),
215 qq{<textarea rows="<">"><foo>&bl'a"\n .</textarea>},
223 [ qw(<select> </select>) ], # malformed html: at least 1 option required
228 [ $form->select(undef, [], '', {name => '', class => ''}) ],
229 [ qw(<select> </select>) ],
230 'explicit empty select'
234 [ $form->select(undef, [undef]) ],
235 [ qw(<select> <option> </select>) ],
240 [ $form->select(foo => [1, 2]) ],
242 '<select id="foo" name="foo">',
243 '<option value="1">', '<option value="2">',
250 [ $form->select(foo => [1, 2], 3) ],
252 '<select id="foo" name="foo">',
253 '<option value="1">', '<option value="2">',
256 'select invalid default'
260 [ $form->select(undef, [1, 2], 2) ],
263 '<option value="1">', '<option selected value="2">',
271 $form->select(foo => [
272 '<">', {value => 2, disabled => 1, selected => 0, class => 1}, {selected => 1}
273 ], '2', {id => '', class => 'a <b', value => 1})
276 '<select class="a <b" name="foo">',
277 '<option value="<">">',
278 '<option selected disabled class="1" value="2">',
286 scalar $form->select(foo => [1, 2]),
287 '<select id="foo" name="foo"><option value="1"><option value="2"></select>',
294 [ $form->select(foo => [1], {type => 'radio'}) ],
295 [ '<input id="foo_1" name="foo" type="radio" value="1">' ],
301 $form->select(foo => [
302 1, {value => 2, name => '', label => ''}, {value => 3, id => '', type => ''}
303 ], {type => 'checkbox', label => {3 => 'test', 2 => 'ignore'}, value => '1'})
306 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
307 '<input id="foo_2" name="" type="checkbox" value="2">',
308 '<label><input name="foo" value="3"> test</label>',
314 $form->radio(foo => 'test'),
315 '<input id="foo_test" name="foo" type="radio" value="test">',
320 $form->radio(foo => undef, 'test'),
321 '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
326 $form->radio(undef, {checked => 1}),
327 '<input checked type="radio">',
328 'simple radio button'
332 [ $form->radio(undef, ['', '<">']) ],
334 '<input type="radio" value="">',
335 '<input type="radio" value="<">">',
341 [ $form->radio(undef, undef, ['', '<">']) ],
343 '<input type="radio" value="1">',
344 '<label><input type="radio" value="2"> <"></label>',
346 'multiple radio labels'
350 [ $form->radio(undef, [0, 1], undef, 0) ],
352 '<input checked type="radio" value="0">',
353 '<input type="radio" value="1">',
359 [ $form->radio(foo => ['foo', ''], 'test', {value => '', id => ''}) ],
361 '<label><input name="foo" type="radio" value="foo"> test</label>',
362 '<label><input checked name="foo" type="radio" value=""> test</label>',
364 'multiple radios with custom value'
368 [ $form->radio(foo => [0, 1, ''], ['', 0]) ],
370 '<input id="foo_0" name="foo" type="radio" value="0">',
371 '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
372 '<input id="foo_" name="foo" type="radio" value="">',
374 'multiple radios with custom values'
378 # make sure arguments aren't modified
379 my @args = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
380 my @orgs = (foo => [0, {value => 1}], [0, 1], {name => 0, value => 1});
382 '<label><input id="foo_0" name="foo" type="radio" value="0"> 0</label>',
383 '<label><input checked id="foo_1" name="foo" type="radio" value="1"> 1</label>',
387 [ $form->radio(@args) ],
389 'options var to radio'
393 [ $form->check(@args) ],
395 '<label><input checked id="foo_1" name="foo" type="checkbox" value="1"> 0</label>',
396 '<input checked id="foo_1" name="foo" type="checkbox" value="1">',
398 'options var to check'
402 scalar $form->radio(@args),
404 'options var again to radio'
407 is_deeply(\@args, \@orgs, 'options var unmodified');
414 '<input id="foo" name="foo" type="checkbox" value="1">',
419 $form->check(foo => '<">'),
420 '<label><input id="foo" name="foo" type="checkbox" value="1"> <"></label>',
425 $form->check(foo => {label => 'test', value => undef}, {disabled => 1}),
426 '<label><input disabled id="foo" name="foo" type="checkbox"> test</label>',
431 [ $form->check(undef, '', 1) ],
432 [ '<input checked type="checkbox" value="1">' ],
437 $form->check(foo => [undef]),
438 '<input id="foo_1" name="foo" type="checkbox" value="1">',
443 [ $form->check(foo => [{value => undef}, undef]) ],
445 '<input id="foo" name="foo" type="checkbox">',
446 '<input id="foo_2" name="foo" type="checkbox" value="2">',
448 'multiple checkboxes'
452 $form->check(foo => [undef], {id => ''}),
453 '<input name="foo" type="checkbox" value="1">',
459 foo => [ {id => 'quux'}, {name => 'name'}, {value => 0}, {id => ''}, undef ], {id => 'bar'}
462 '<input id="quux" name="foo" type="checkbox" value="1">',
463 '<input id="bar_2" name="name" type="checkbox" value="2">',
464 '<input id="bar_0" name="foo" type="checkbox" value="0">',
465 '<input name="foo" type="checkbox" value="4">',
466 '<input id="bar_5" name="foo" type="checkbox" value="5">',
472 [ $form->check(foo => ['bar', {name => 'bar'}], {name => 'ignored'}) ],
474 '<label><input id="foo_1" name="foo" type="checkbox" value="1"> bar</label>',
475 '<input id="foo_2" name="bar" type="checkbox" value="2">',
481 [ $form->check(undef, ['', '<">']) ],
483 '<input type="checkbox" value="1">',
484 '<label><input type="checkbox" value="2"> <"></label>',
486 'anonymous checkboxes'
490 [ $form->check(undef, [{}, undef], 1) ],
492 '<input checked type="checkbox" value="1">',
493 '<input checked type="checkbox" value="2">',
495 'multiple checked checkboxes'
501 scalar $form->check(foo => [1, 0], {value => 0}),
503 '<label><input id="foo_1" name="foo" type="checkbox" value="1"> 1</label>',
504 '<label><input id="foo_2" name="foo" type="checkbox" value="2"> 0</label>',
511 [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
513 '<input checked type="radio" value="4">',
514 '<input type="checkbox" value="2">',
515 '<input checked type="checkbox" value="3">',
522 my $defform = HTML::Form::Simple->new({foo => '<">', '' => 'empty', 0 => 0});
523 ok($defform, 'form with defaults');
526 $defform->hidden(''),
527 '<input name="" type="hidden" value="empty">',
528 'hidden with default'
532 $defform->hidden(undef),
533 '<input type="hidden">',
538 $defform->text('foo'),
539 '<input id="foo" name="foo" type="text" value="<">">',
544 $defform->text('foo', {value => 'custom'}),
545 '<input id="foo" name="foo" type="text" value="custom">',
546 'input with value and default'
550 [ $defform->radio(0 => [1, 0]) ],
552 '<input id="0_1" name="0" type="radio" value="1">',
553 '<input checked id="0_0" name="0" type="radio" value="0">',
555 'select with default'