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;
use Test::More;
-plan tests => 47;
+plan tests => 50;
use_ok('HTML::Form::Simple');
);
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"> <"></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