XXX: multiple radio values
authorMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 21:03:42 +0000 (23:03 +0200)
committerMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 21:33:28 +0000 (23:33 +0200)
lib/HTML/Form/Simple.pm
t/html.t

index 2c8ce8de0d2938855c6fed97f31fb4c5ee819b8b..6fb908046cc538e16210b77f4f9b46436ceec56d 100644 (file)
@@ -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 {
index 60e95074768d395f2567cac743440c6903576eb6..b7568b079cfa9ea14bfc9d0300c9d5d78009e008 100644 (file)
--- 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),
-       '<input id="foo_1" name="foo" type="radio" value="1">',
+       $form->radio(foo => 'test'),
+       '<label><input id="foo_1" name="foo" type="radio" value="1"> test</label>',
        'radio method'
 );
 
+is(
+       $form->radio(foo => undef, [2]), #TODO
+       '<input id="foo_2" name="foo" type="radio" value="2">',
+       'labelless radio'
+);
+
 is(
        $form->radio(undef, {checked => 1}),
        '<input checked type="radio">',
        'simple radio button'
 );
 
+is_deeply(
+       [ $form->radio(undef, ['', '<">']) ],
+       [
+               '<input type="radio" value="1">',
+               '<label><input type="radio" value="2"> <"></label>',
+       ],
+       'multiple radios'
+);
+
+is_deeply(
+       [ $form->radio(foo => 'test', ['foo', ''], {value => '', id => ''}) ],
+       [
+               '<label><input name="foo" type="radio" value="foo"> test</label>',
+               '<label><input checked name="foo" type="radio" value=""> test</label>',
+       ],
+       'multiple radios with custom value'
+);
+
+is_deeply(
+       [ $form->radio(foo => ['', 0], [0, 1, '']) ],
+       [
+               '<input id="foo_0" name="foo" type="radio" value="0">',
+               '<label><input id="foo_1" name="foo" type="radio" value="1"> 0</label>',
+               '<input id="foo_" name="foo" type="radio" value="">',
+       ],
+       'multiple radios with custom values'
+);
+
+# check
+
 is(
        $form->check('foo'),
        '<input id="foo_1" name="foo" type="checkbox" value="1">',