X-Git-Url: http://git.shiar.net/perl/html-form-simple.git/blobdiff_plain/b01a7f21ade2de12b620970a9e7ae78cbabd95da..48891d33a852d6db31113e4e7c10e1cda5771ce3:/lib/HTML/Form/Simple.pm diff --git a/lib/HTML/Form/Simple.pm b/lib/HTML/Form/Simple.pm index 2c8ce8d..911b864 100644 --- a/lib/HTML/Form/Simple.pm +++ b/lib/HTML/Form/Simple.pm @@ -71,13 +71,24 @@ sub hidden { my $self = shift; my ($name, $value, $attr) = $self->_attr(2, @_); - $attr = {type => 'hidden', name => $name, value => $value}; - #TODO: $attr + if (ref $name eq 'HASH') { + my @return = map { $self->hidden($_, $name->{$_}, $attr) } sort keys %$name; + return wantarray ? @return : join(defined $, ? $, : '', @return); + } + + if (ref $value eq 'ARRAY') { + my @return = map { $self->hidden($name, $_, $attr) } @$value; + return wantarray ? @return : join(defined $, ? $, : '', @return); + } + + $attr->{name } = $name if defined $name; + $attr->{value} = $value if defined $value; + $attr->{type} = 'hidden' unless defined $attr->{type}; return $self->tag(input => $attr); } -sub input { +sub text { my $self = shift; my ($name, $value, $attr) = $self->_attr(2, @_); @@ -107,6 +118,8 @@ sub select { my @options = map { ref $_ ? $_ : {value => $_} } @$rows; + my @return; + if ($attr->{type} eq 'select') { delete $attr->{type}; if (defined $default) { @@ -114,17 +127,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}) { - defined $_->{id} or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value} - for @options; + if (defined $attr->{id} and $attr->{id} ne '') { + defined $_->{id} + or defined $_->{value} and $_->{id} = $attr->{id}.'_'.$_->{value} + for @options; } if (defined $attr->{label}) { defined $_->{value} and not defined $_->{label} @@ -138,39 +151,70 @@ 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 { my $self = shift; - my ($name, $value, $attr) = $self->_attr(2, @_); + my ($name, $label, $rows, $attr) = $self->_attr(3, @_); - $self->select($name, [$value], {%$attr, type => 'radio'}); + if (not defined $rows) { + if (defined $label) { + $rows = ref $label eq 'ARRAY' ? [1 .. $#$label+1] : [1]; + } + else { + $rows = [{}]; + } + } + elsif (ref $rows ne 'ARRAY') { + $rows = [$rows]; + } + + if (defined $label) { + $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, $rows, {%$attr, type => 'radio'}); } sub check { my $self = shift; my ($name, $label, $checked, $attr) = $self->_attr(3, @_); - $attr->{label } = $label if defined $label; - $attr->{checked} = $checked if defined $checked; - $attr->{value } = '1' unless exists $attr->{value}; + 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') { + $rows->[$_]->{checked} = $checked->[$_] for 0 .. $#$rows; + push @$rows, map { {checked => $_} } @$checked[@$rows .. $#$checked]; + } + else { + $_->{checked} = $checked for @$rows; + } + } + exists $rows->[$_]->{value} or $rows->[$_]->{value} = $_ + 1 for 0 .. $#$rows; - $self->select($name, [$attr], {type => 'checkbox'}); + $self->select($name, $rows, {%$attr, type => 'checkbox'}); } 1; =head1 NAME -HTML::Form::Simple +HTML::Form::Simple - Generate HTML form elements =head1 SYNOPSIS @@ -195,3 +239,12 @@ HTML::Form::Simple ); say $input->stop; # +=head1 AUTHOR + +Mischa POSLAWSKY + +=head1 LICENSE + +This module is free software; you can redistribute it and/or modify it +under the same L as Perl itself. +