XXX: multiple check values
authorMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 22:48:30 +0000 (00:48 +0200)
committerMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 22:48:30 +0000 (00:48 +0200)
lib/HTML/Form/Simple.pm
t/html.t

index 6fb908046cc538e16210b77f4f9b46436ceec56d..4eddc98188b7c543653465b52347dbc8fb1ee431 100644 (file)
@@ -176,11 +176,20 @@ sub check {
        my $self = shift;
        my ($name, $label, $checked, $attr) = $self->_attr(3, @_);
 
-       $attr->{label  } = $label   if defined $label;
-       $attr->{checked} = $checked if defined $checked;
-       $attr->{value  } = '1' unless exists $attr->{value};
+       my $rows = defined $label ? ref $label eq 'ARRAY' ? $label : [$label] : [{}];
+       ref $_ eq 'HASH' or $_ = {label => $_} for @$rows;
+       if (defined $checked) {
+               if (ref $checked eq 'ARRAY') {
+                       $_->{checked} = shift @$checked for @$rows;
+                       push @$rows, map { {checked => $_} } @$checked;
+               }
+               else {
+                       $_->{checked} = $checked for @$rows;
+               }
+       }
+       exists $rows->[$_]->{value} or $rows->[$_]->{value} = $_ + 1 for 0 .. $#$rows;
 
-       $self->select($name, [$attr], {type => 'checkbox'});
+       $self->select($name, $rows, {%$attr, type => 'checkbox'});
 }
 
 1;
index b7568b079cfa9ea14bfc9d0300c9d5d78009e008..c646cdb9810cbcb60314445f6cb2d3c8284825f5 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 47;
+plan tests => 50;
 
 use_ok('HTML::Form::Simple');
 
@@ -330,28 +330,50 @@ is(
 );
 
 is(
-       $form->check(foo => 'test', {value => undef}),
-       '<label><input id="foo" name="foo" type="checkbox"> test</label>',
-       'check parameters'
+       $form->check(foo => '<">'),
+       '<label><input id="foo_1" name="foo" type="checkbox" value="1"> <"></label>',
+       'labeled checkbox'
 );
 
 is(
-       $form->check(undef, '', 1),
-       '<input checked type="checkbox" value="1">',
-       'simple checkbox'
+       $form->check(foo => {label => 'test', value => undef}, {disabled => 1}),
+       '<label><input disabled id="foo" name="foo" type="checkbox"> test</label>',
+       'check parameters'
+);
+
+is_deeply(
+       [ $form->check(undef, '', 1) ],
+       [ '<input checked type="checkbox" value="1">' ],
+       'anonymous checkbox'
 );
 
-TODO: {
-       local $TODO = 'shorthand';
 is_deeply(
        [ $form->check(undef, ['', '<">']) ],
        [
-               '<input checked type="checkbox" value="1">',
-               '<label><input checked type="checkbox" value="2"> &lt;&quot;></label>',
+               '<input type="checkbox" value="1">',
+               '<label><input type="checkbox" value="2"> <"></label>',
        ],
        'multiple checkboxes'
 );
-};
+
+is_deeply(
+       [ $form->check(undef, [{}, undef], 1) ],
+       [
+               '<input checked type="checkbox" value="1">',
+               '<input checked type="checkbox" value="2">',
+       ],
+       'multiple checked checkboxes'
+);
+
+is_deeply(
+       [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
+       [
+               '<input checked type="radio" value="4">',
+               '<input type="checkbox" value="2">',
+               '<input checked type="checkbox" value="3">',
+       ],
+       'various checkboxes'
+);
 
 #TODO