From: Mischa Poslawsky Date: Fri, 25 Jul 2008 21:03:42 +0000 (+0200) Subject: XXX: multiple radio values X-Git-Url: http://git.shiar.net/perl/html-form-simple.git/commitdiff_plain/d5415f8292000c0eba35458ce61500eaff9cd4d1 XXX: multiple radio values --- diff --git a/lib/HTML/Form/Simple.pm b/lib/HTML/Form/Simple.pm index 2c8ce8d..6fb9080 100644 --- a/lib/HTML/Form/Simple.pm +++ b/lib/HTML/Form/Simple.pm @@ -122,7 +122,7 @@ sub select { return wantarray ? @return : join('', @return); } else { - if (defined $attr->{id}) { + if (defined $attr->{id} and $attr->{id} ne '') { defined $_->{id} or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value} for @options; } @@ -150,9 +150,26 @@ sub select { sub radio { my $self = shift; - my ($name, $value, $attr) = $self->_attr(2, @_); + my ($name, $label, $value, $attr) = $self->_attr(3, @_); + + if (not defined $value) { + if (defined $label) { + $value = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1]; + } + else { + $value = [{}]; + } + } + elsif (ref $value ne 'ARRAY') { + $value = [$value]; + } + + if (defined $label) { + $_ = ref $_ eq 'HASH' ? {%$_} : {value => $_} for @$value; + $_->{label} = ref $label eq 'ARRAY' ? shift @$label : $label for @$value; + } - $self->select($name, [$value], {%$attr, type => 'radio'}); + $self->select($name, $value, {%$attr, type => 'radio'}); } sub check { diff --git a/t/html.t b/t/html.t index 60e9507..b7568b0 100644 --- a/t/html.t +++ b/t/html.t @@ -5,7 +5,7 @@ use warnings; use Test::More; -plan tests => 43; +plan tests => 47; use_ok('HTML::Form::Simple'); @@ -276,17 +276,53 @@ is_deeply( ); is( - $form->radio(foo => 1), - '', + $form->radio(foo => 'test'), + '', 'radio method' ); +is( + $form->radio(foo => undef, [2]), #TODO + '', + 'labelless radio' +); + is( $form->radio(undef, {checked => 1}), '', 'simple radio button' ); +is_deeply( + [ $form->radio(undef, ['', '<">']) ], + [ + '', + '', + ], + 'multiple radios' +); + +is_deeply( + [ $form->radio(foo => 'test', ['foo', ''], {value => '', id => ''}) ], + [ + '', + '', + ], + 'multiple radios with custom value' +); + +is_deeply( + [ $form->radio(foo => ['', 0], [0, 1, '']) ], + [ + '', + '', + '', + ], + 'multiple radios with custom values' +); + +# check + is( $form->check('foo'), '',