Subject: | lookup_options does not work with derived columns |
Perl version 5.10.1
Package HTML::FormHandler::Model::DBIC, version 0.12
Linux apache 2.6.32-23-generic #37-Ubuntu SMP Fri Jun 11 07:54:58 UTC
2010 i686 GNU/Linux
---
If the "label_name" attribute for a field of type "Select" or "Multiple"
refers to the name of a method in the DBIx::Class result class, rather
than the name of a column, "lookup_options" does not return a list of
options.
For example, if my DBIx::Class result class has the following column
definitions:
__PACKAGE__->add_columns(
'id', {data_type => 'integer', is_auto_increment => 1},
'first_name', {data_type => 'varchar', size => 255},
'last_name', {data_type => 'varchar', size => 255},
);
And it has a method to generate the full name from the first and last names:
sub full_name {
my $self = shift;
return = $self->first_name . ' ' . $self->last_name;
}
The "full_name" method cannot be used as the "label_column" in a field
of type "Multiple" or "Select":
# Using the "full_name" method does not work:
has_field 'full_name' => (type => 'Multiple', label_column =>
'full_name');
# Using a column name does work:
has_field 'full_name' => (type => 'Multiple', label_column =>
'last_name');
The problem occurs because of these lines (315, 316) in the
"lookup_options" method:
my $label_column = $field->label_column;
return unless $source->has_column($label_column);
"$source->has_column($label_column)" returns false in when
"$label_column" is a method rather than a column.
The following solution works for me, but I don't know if it will present
other problems. Starting at line 315 in HTML::FormHandler::Model::DBIC,
change:
my $label_column = $field->label_column;
return unless $source->has_column($label_column);
my $active_col = $self->active_column || $field->active_column;
$active_col = '' unless $source->has_column($active_col);
my $sort_col = $field->sort_column;
$sort_col = defined $sort_col && $source->has_column($sort_col) ?
$sort_col : $label_column;
my ($primary_key) = $source->primary_columns;
To:
my $label_column = $field->label_column;
my $active_col = $self->active_column || $field->active_column;
$active_col = '' unless $source->has_column($active_col);
my $sort_col = $field->sort_column;
my ($primary_key) = $source->primary_columns;
if (!(defined $sort_col && $source->has_column($sort_col))) {
$sort_col =
$source->has_column($label_column) ?
$label_column :
$primary_key;
}
It is not possible to order the results by a derived column, so if no
suitable sort column is specified, the label column is used, but only if
it is a real column. Otherwise, the primary key column is used.
My very limited testing has shown that this works correctly.