From 777bf75cfcfc81a87479a508ea900de4a64c483e Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 13 Dec 2007 14:17:35 +0000 Subject: [PATCH] =?utf8?q?Eine=20Hilfsfunktion,=20die=20aus=20Array-=20und?= =?utf8?q?=20Hashstrukturen=20in=20$form=20eine=20Liste=20von=20Variablenn?= =?utf8?q?amen=20und=20Werten=20erzeugt,=20die=20dann=20wieder=20als=20ver?= =?utf8?q?steckte=20Inputs=20in=20HTML-Formularen=20ausgegeben=20werden=20?= =?utf8?q?kann.=20Dabei=20sind=20die=20Variablennamen=20strukturiert=20(so?= =?utf8?q?=20wird=20z.B.=20aus=20"$form->{filter}->[0]->{description}"=20d?= =?utf8?q?er=20Name=20"filter[+].description").=20Au=C3=9Ferdem=20eine=20A?= =?utf8?q?npassung=20von=20$form->isblank(),=20die=20solch=20strukturierte?= =?utf8?q?=20Variablennamen=20versteht.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Form.pm | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/SL/Form.pm b/SL/Form.pm index d9d79a511..f893c63aa 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -224,6 +224,58 @@ sub new { return $self; } +sub _flatten_variables_rec { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my $curr = shift; + my $prefix = shift; + my $key = shift; + + my @result; + + if ('' eq ref $curr->{$key}) { + @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} }); + + } elsif ('HASH' eq ref $curr->{$key}) { + foreach my $hash_key (sort keys %{ $curr->{$key} }) { + push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key); + } + + } else { + foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) { + my $first_array_entry = 1; + + foreach my $hash_key (sort keys %{ $curr->{$key}->[$idx] }) { + push @result, $self->_flatten_variables_rec($curr->{$key}->[$idx], $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key); + $first_array_entry = 0; + } + } + } + + $main::lxdebug->leave_sub(2); + + return @result; +} + +sub flatten_variables { + $main::lxdebug->enter_sub(2); + + my $self = shift; + my @keys = @_; + + my @variables; + + foreach (@keys) { + push @variables, $self->_flatten_variables_rec($self, '', $_); + } + + $main::lxdebug->leave_sub(2); + + return @variables; +} + + sub debug { $main::lxdebug->enter_sub(); @@ -395,9 +447,14 @@ sub isblank { my ($self, $name, $msg) = @_; - if ($self->{$name} =~ /^\s*$/) { - $self->error($msg); + my $curr = $self; + foreach my $part (split '.', $name) { + if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) { + $self->error($msg); + } + $curr = $curr->{$part}; } + $main::lxdebug->leave_sub(); } -- 2.20.1