From 745badbb1417451398a0f983c450fd8725794f65 Mon Sep 17 00:00:00 2001 From: biopete Date: Fri, 21 Jul 2006 15:28:31 +0000 Subject: [PATCH] bug in options_from_hashes git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@516 48953598-375a-da11-a14b-00016c27c3ee --- lib/Maypole/Model/CDBI/AsForm.pm | 42 ++++++++++++++------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index ce26226..cd295f3 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -315,7 +315,7 @@ sub to_cgi { This maps an individual column to a form element. The C argument can be used to force the field type into any you want. All that you need is a method named "_to_$how" in your class. Your class inherits many from -AsForm already. Override them at will. +AsForm already. If C is specified but the class cannot call the method it maps to, then AsForm will issue a warning and the default input will be made. @@ -331,8 +331,8 @@ sub to_field { unless ($how) { $how = $args->{how} || ''; } #warn "In to_field field is $field how is $how. args ar e" . Dumper($args) . " \n"; # Set sensible default value - unless ($args->{default}) { - my $def = $self->column_default($field); + if ($field and not defined $args->{default}) { + my $def = $self->column_default($field) ; # exclude defaults we don't want actually put as value for input if (defined $def) { $def = $def =~ /(^0000-00-00.*$|^0[0]*$|^0\.00$|CURRENT_TIMESTAMP|NULL)/i ? '' : $def ; @@ -518,18 +518,6 @@ sub _field_from_relationship { return; } - - - #NOOO! maybe select from has_many -# if ($rel_type eq 'has_many' and ref $self) { -# $args->{items} ||= [$self->$field]; -# # arg name || fclass pk name || field -# if (not $args->{name}) { -# $args->{name} = eval{$fclass->primary_column->name} || $field; -# } -# return $self->_to_select($field, $args); -# } - # # maybe foreign inputs my %local_cols = map { $_ => 1 } $self->columns; # includes is_a cols if ($fclass_is_cdbi and (not $local_cols{$field} or $rel_name eq 'has_own')) @@ -549,8 +537,11 @@ Override at will. sub _field_from_column { my ($self, $field, $args) = @_; - return unless $field; - my $class = ref $self || $self; + # this class and pk are default class and field at this point + my $class = $args->{class} || $self; + $class = ref $class || $class; + $field ||= ($class->primary_columns)[0]; # TODO + # Get column type unless ($args->{column_type}) { if ($class->can('column_type')) { @@ -580,6 +571,9 @@ sub _field_from_column { sub _to_textarea { my ($self, $col, $args) = @_; + my $class = $args->{class} || $self; + $class = ref $class || $class; + $col ||= ($class->primary_columns)[0]; # TODO # pjs added default $args ||= {}; my $val = $args->{value}; @@ -744,6 +738,7 @@ sub _to_select { if not $args->{selected} and ref $self; } $col = $args->{class}->primary_column; + $args->{name} ||= $col; } # Related Class maybe ? elsif ($rel_meta = $self->related_meta('r:)', $col) ) { @@ -783,11 +778,10 @@ sub _to_select { } # We could say :Col is name and we are selecting out of class arg. # DIE for now - else { - #$args->{name} = $col; - die "Usage _to_select. $col not related to any class to select from. "; + #else { + # die "Usage _to_select. $col not related to any class to select from. "; - } + #} # Set arguments unless ( defined $args->{column_nullable} ) { @@ -1317,13 +1311,13 @@ sub _options_from_hashes { my $fclass = $args->{class} || ''; my $stringify = $args->{stringify} || ''; my @res; - for (@$items) { - my $val = defined $_->{$pk} ? $_->{$pk} : ''; + for my $item (@$items) { + my $val = defined $item->{$pk} ? $item->{$pk} : ''; my $opt = HTML::Element->new("option", value => $val); $opt->attr(selected => "selected") if $selected->{$val}; my $content = ($fclass and $stringify and $fclass->can($stringify)) ? $fclass->$stringify($_) : - join(' ', keys %$_); + join(' ', map {$item->{$_} } keys %$item); $opt->push_content( $content ); push @res, $opt; } -- 2.39.2