sub radio {
my $self = shift;
- my ($name, $label, $value, $attr) = $self->_attr(3, @_);
+ my ($name, $label, $rows, $attr) = $self->_attr(3, @_);
- if (not defined $value) {
+ if (not defined $rows) {
if (defined $label) {
- $value = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1];
+ $rows = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1];
}
else {
- $value = [{}];
+ $rows = [{}];
}
}
- elsif (ref $value ne 'ARRAY') {
- $value = [$value];
+ elsif (ref $rows ne 'ARRAY') {
+ $rows = [$rows];
}
if (defined $label) {
- $_ = ref $_ eq 'HASH' ? {%$_} : {value => $_} for @$value;
- $_->{label} = ref $label eq 'ARRAY' ? shift @$label : $label for @$value;
+ $rows = [ map { ref $_ eq 'HASH' ? {%$_} : {value => $_} } @$rows ];
+ if (ref $label eq 'ARRAY') {
+ $rows->[$_]->{label} = $label->[$_] for 0 .. $#$rows;
+ } else {
+ $_->{label} = $label for @$rows;
+ }
}
- $self->select($name, $value, {%$attr, type => 'radio'});
+ $self->select($name, $rows, {%$attr, type => 'radio'});
}
sub check {
my $self = shift;
my ($name, $label, $checked, $attr) = $self->_attr(3, @_);
- my $rows = defined $label ? ref $label eq 'ARRAY' ? $label : [$label] : [{}];
+ 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;
+ $rows->[$_]->{checked} = $checked->[$_] for 0 .. $#$rows;
+ push @$rows, map { {checked => $_} } @$checked[@$rows .. $#$checked];
}
else {
$_->{checked} = $checked for @$rows;
use Test::More;
-plan tests => 55;
+plan tests => 59;
use_ok('HTML::Form::Simple');
'multiple radios with custom values'
);
+{
+ # make sure arguments aren't modified
+ my @args = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1});
+ my @orgs = (foo => [0, 1], [0, {value => 1}], {name => 0, value => 1});
+ my @output = (
+ '<label><input id="foo_0" name="foo" type="radio" value="0"> 0</label>',
+ '<label><input checked id="foo_1" name="foo" type="radio" value="1"> 1</label>',
+ );
+
+ is_deeply(
+ [ $form->radio(@args) ],
+ \@output,
+ 'options var to radio'
+ );
+
+ is_deeply(
+ [ $form->check(@args) ],
+ [
+ '<label><input checked id="foo_1" name="foo" type="checkbox" value="1"> 0</label>',
+ '<label><input checked id="foo_2" name="foo" type="checkbox" value="2"> 1</label>',
+ ],
+ 'options var to check'
+ );
+
+ is(
+ scalar $form->radio(@args),
+ join('', @output),
+ 'options var again to radio'
+ );
+
+ is_deeply(\@args, \@orgs, 'options var unmodified');
+}
+
# check
is(