This maps an individual column to a form element. The C<how> 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<how> 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.
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 ;
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'))
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')) {
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};
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) ) {
}
# 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} ) {
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;
}