From: Mischa Poslawsky Date: Fri, 25 Jul 2008 23:31:52 +0000 (+0200) Subject: join multiple select results by $OFS X-Git-Url: http://git.shiar.net/perl/html-form-simple.git/commitdiff_plain/6bef855dc6c753640512d512e5b31e2734484382 join multiple select results by $OFS 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. --- diff --git a/lib/HTML/Form/Simple.pm b/lib/HTML/Form/Simple.pm index 66958c3..5946030 100644 --- a/lib/HTML/Form/Simple.pm +++ b/lib/HTML/Form/Simple.pm @@ -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), '', ); - 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 '' ? '" : $self->tag(input => $_) } @options; - return wantarray ? @return : join('', @return); } + + return wantarray ? @return : join(defined $, ? $, : '', @return); } sub radio { diff --git a/t/html.t b/t/html.t index e5abb9a..8ab4806 100644 --- 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), '', 'labelless radio' ); @@ -369,6 +369,18 @@ is_deeply( 'multiple checked checkboxes' ); +{ + local $, = ' | '; + is( + scalar $form->check(foo => [1, 0], {value => 0}), + join(' | ', + '', + '', + ), + 'merged checkboxes' + ); +} + is_deeply( [ $form->check(undef, [{value => 4, type => 'radio'}], [1, 0, 0], {value => 3}) ], [