From 346143529922475f513453053cab4b387ac22b9d Mon Sep 17 00:00:00 2001 From: biopete Date: Tue, 9 May 2006 04:38:32 +0000 Subject: [PATCH] Bug fixxes to AsForm -- has_many select and zero filled pks not being selected git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@488 48953598-375a-da11-a14b-00016c27c3ee --- lib/Maypole/Model/CDBI/AsForm.pm | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index 9feed0d..6ca422a 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -430,7 +430,8 @@ sub _field_from_relationship { if (not $rel_meta->{args}{no_select} and not $args->{no_select}) { $args->{class} = $fclass; - $args->{items} = $self->$field; + my @itms = $self->$field; # need list not iterator + $args->{items} = \@itms; return $self->_to_select($field, $args); } return; @@ -647,7 +648,7 @@ sub _to_textfield { sub _to_select { my ($self, $col, $args) = @_; $args ||= {}; -# Do we have items already ? Go no further. + # Do we have items already ? Go no further. if ($args->{items} and ref $args->{items}) { my $a = $self->_select_guts($col, $args); $OLD_STYLE && return $a->as_HTML; @@ -655,7 +656,7 @@ sub _to_select { return $a; } -# Else what are we making a select box out of ? + # Else what are we making a select box out of ? # No Column parameter -- means making a select box of args->class or self # Using all rows from class's table if (not $col) { @@ -673,7 +674,7 @@ sub _to_select { # related objects pre selected if object # "Has many" -- Issues: - # 1) want to select one from list if self is an object + # 1) want to select one or many from list if self is an object # Thats about all we can do really, # 2) except for mapping which is TODO and would # do something like add to and take away from list of permissions for @@ -681,7 +682,8 @@ sub _to_select { # Hasmany select one from list if ref self if ($rel_meta->{name} =~ /has_many/i and ref $self) { - $args->{items} = [ $self->$col ]; + my @itms = $self->$col; # need list not iterator + $args->{items} = \@itms; my $a = $self->_select_guts($col, $args); $OLD_STYLE && return $a->as_HTML; return $a; @@ -1024,12 +1026,15 @@ Below handles these formats for the "selected" slot in the arguments hash: sub _hash_selected { my ($args) = shift; my $selected = $args->{value} || $args->{selected}; - return $selected unless $selected and ref $selected ne 'HASH'; - #warn "Selected dump : " . Dumper($selected); + #warn "**** SELECTED is $selected ****"; my $type = ref $selected; + return $selected unless $selected and $type ne 'HASH'; + #warn "Selected dump : " . Dumper($selected); # Single Object if ($type and $type ne 'ARRAY') { - return {$selected->id => 1}; + my $id = $selected->id; + $id =~ s/^0*//; + return {$id => 1}; } # Single Scalar id elsif (not $type) { @@ -1085,7 +1090,9 @@ sub _select_guts { my ($self, $col, $args) = @_; #$nullable, $selected_id, $values) = @_; #$args->{stringify} ||= 'stringify_selectbox'; + $args->{selected} = _hash_selected($args) if defined $args->{selected}; + warn "*** Dumpe of selected " . Dumper( $args->{selected} ); my $name = $args->{name} || $col; my $a = HTML::Element->new('select', name => $name); $a->attr( %{$args->{attr}} ) if $args->{attr}; @@ -1157,8 +1164,10 @@ sub _options_from_objects { my $stringify = $args->{stringify} || ''; my @res; for (@$items) { - my $opt = HTML::Element->new("option", value => $_->id); - $opt->attr(selected => "selected") if $selected->{$_->id}; + my $id = $_->id; + my $opt = HTML::Element->new("option", value => $id); + $id =~ s/^0*//; # leading zeros no good in hash key + $opt->attr(selected => "selected") if $selected->{$id}; my $content = $stringify ? $_->stringify : "$_"; $opt->push_content($content); push @res, $opt; -- 2.39.5