Subject: | Patch for Class::DBI::AsForm for displaying enum values |
Hello!
Here is a patch for Class/DBI/AsForm to enable support
for dropdown lists ("select" controls) for ENUM fields.
Works well for MySQL.
For other DBMS - not checked.
The only problem for MySQL is that Class::DBI::mysql::column_type
returns 'char' for enum fields, at least in my environment.
So I'm forced to check for enum_vals for char fields also.
But anyway it works correctly.
Subject: | Class-DBI-AsForm.pm.diff |
--- AsForm.pm.orig 2007-06-04 13:43:16.000000000 +0500
+++ AsForm.pm 2007-06-04 14:18:45.000000000 +0500
@@ -96,6 +96,15 @@
# Right, have some of this!
eval "package $class; Class::DBI::Plugin::Type->import()";
my $type = $class->column_type($field);
+
+ my @enum_allowed;
+ if ($type && $type =~ /^(CHAR|ENUM)$/i) {
+ @enum_allowed = eval { $class->enum_vals($field) };
+ }
+
+ return $self->_to_select_enum($field, \@enum_allowed)
+ if scalar @enum_allowed;
+
return $self->_to_textarea($field)
if $type
and $type =~ /^(TEXT|BLOB)$/i;
@@ -137,6 +146,22 @@
$a;
}
+sub _to_select_enum {
+ my ($self, $col, $enum_allowed) = @_;
+
+ my $a = HTML::Element->new("select", name => $col);
+ for my $enum_value (@$enum_allowed) {
+ my $sel = HTML::Element->new("option", value => $enum_value);
+ my $curval = eval { $self->$col };
+ $sel->attr("selected" => "selected")
+ if $enum_value eq $curval;
+ $sel->push_content($enum_value);
+ $a->push_content($sel);
+ }
+ $OLD_STYLE && return $a->as_HTML;
+ $a;
+}
+
1;
=head1 CHANGES