join multiple select results by $OFS
authorMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 23:31:52 +0000 (01:31 +0200)
committerMischa Poslawsky <mischa@mediadesign.nl>
Fri, 25 Jul 2008 23:39:41 +0000 (01:39 +0200)
In scalar context, put $OUTPUT_FIELD_SEPARATOR ($,) between each option,
which under normal circumstances is unset, resulting in unaltered
behaviour.  This allows custom formatting without the need for special
attribute options/misuse.

lib/HTML/Form/Simple.pm
t/html.t

index 66958c3c294d760d7729474a4ba829de9e3e1b10..5946030fc4d1a7eda47d3e28aed25f50f3defbdf 100644 (file)
@@ -108,6 +108,8 @@ sub select {
 
        my @options = map { ref $_ ? $_ : {value => $_} } @$rows;
 
+       my @return;
+
        if ($attr->{type} eq 'select') {
                delete $attr->{type};
                if (defined $default) {
@@ -115,17 +117,17 @@ sub select {
                                $_->{selected} = 1 if defined $_->{value} and $_->{value} eq $default;
                        }
                }
-               my @return = (
+               @return = (
                        $self->tag(select => $attr),
                        (map { $self->tag(option => $_) } @options),
                        '</select>',
                );
-               return wantarray ? @return : join('', @return);
        }
        else {
                if (defined $attr->{id} and $attr->{id} ne '') {
-                       defined $_->{id} or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value}
-                               for @options;
+                       defined $_->{id}
+                               or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value}
+                                       for @options;
                }
                if (defined $attr->{label}) {
                        defined $_->{value} and not defined $_->{label}
@@ -139,14 +141,15 @@ sub select {
                        }
                }
                $_ = {%$attr, %$_} for @options;
-               my @return = map {
+               @return = map {
                        my $label = delete $_->{label};
                        defined $label && $label ne ''
                                ? '<label>'.$self->tag(input => $_)." $label</label>"
                                :           $self->tag(input => $_)
                } @options;
-               return wantarray ? @return : join('', @return);
        }
+
+       return wantarray ? @return : join(defined $, ? $, : '', @return);
 }
 
 sub radio {
index e5abb9ac5ac34e2e4550dc4c52e4d590043ff1b6..8ab4806da4e9910cde6fd2c9deefc3b30cf2bf60 100644 (file)
--- a/t/html.t
+++ b/t/html.t
@@ -5,7 +5,7 @@ use warnings;
 
 use Test::More;
 
-plan tests => 51;
+plan tests => 52;
 
 use_ok('HTML::Form::Simple');
 
@@ -286,7 +286,7 @@ is(
 );
 
 is(
-       $form->radio(foo => undef, [2]), #TODO
+       $form->radio(foo => undef, 2),
        '<input id="foo_2" name="foo" type="radio" value="2">',
        'labelless radio'
 );
@@ -369,6 +369,18 @@ is_deeply(
        'multiple checked checkboxes'
 );
 
+{
+       local $, = ' | ';
+       is(
+               scalar $form->check(foo => [1, 0], {value => 0}),
+               join(' | ',
+                       '<label><input id="foo_1" name="foo" type="checkbox" value="1"> 1</label>',
+                       '<label><input id="foo_2" name="foo" type="checkbox" value="2"> 0</label>',
+               ),
+               'merged checkboxes'
+       );
+}
+
 is_deeply(
        [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ],
        [