From cb6c92feb70f4246804078114bb8ef37874ab1cd Mon Sep 17 00:00:00 2001 From: Mischa Poslawsky Date: Sat, 26 Jul 2008 00:48:30 +0200 Subject: [PATCH] XXX: multiple check values --- lib/HTML/Form/Simple.pm | 17 +++++++++++---- t/html.t | 46 ++++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/lib/HTML/Form/Simple.pm b/lib/HTML/Form/Simple.pm index 6fb9080..4eddc98 100644 --- a/lib/HTML/Form/Simple.pm +++ b/lib/HTML/Form/Simple.pm @@ -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; diff --git a/t/html.t b/t/html.t index b7568b0..c646cdb 100644 --- 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}), - '', - 'check parameters' + $form->check(foo => '<">'), + '', + 'labeled checkbox' ); is( - $form->check(undef, '', 1), - '', - 'simple checkbox' + $form->check(foo => {label => 'test', value => undef}, {disabled => 1}), + '', + 'check parameters' +); + +is_deeply( + [ $form->check(undef, '', 1) ], + [ '' ], + 'anonymous checkbox' ); -TODO: { - local $TODO = 'shorthand'; is_deeply( [ $form->check(undef, ['', '<">']) ], [ - '', - '', + '', + '', ], 'multiple checkboxes' ); -}; + +is_deeply( + [ $form->check(undef, [{}, undef], 1) ], + [ + '', + '', + ], + 'multiple checked checkboxes' +); + +is_deeply( + [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ], + [ + '', + '', + '', + ], + 'various checkboxes' +); #TODO -- 2.30.0