package Class::DBI::AsForm; use 5.006; use strict; use warnings; use Class::DBI::Plugin::Type (); # Bug -- column_type returns char for enum cols # Use column_type in model class instead use Data::Dumper; # pjs for use Class::DBI::mysql (); our $OLD_STYLE = 0; use HTML::Element; require Exporter; our @ISA = qw(Exporter); # PETER SPELTZ -- Added new "_to_*" methods to @EXPORT our @EXPORT = qw( to_cgi to_field _to_textarea _to_textfield _to_select type_of _to_foreign_inputs _to_enum_select _to_bool_select ); our $VERSION = '2.3'; =head1 NAME Class::DBI::AsForm Produce HTML form elements for database columns =head1 SYNOPSIS package Music::CD; use Class::DBI::AsForm; use base 'Class::DBI'; use CGI; ... sub create_or_edit { my $class = shift; my %cgi_field = $class->to_cgi; return start_form, (map { "$_: ". $cgi_field{$_}->as_HTML."
" } $class->Columns), end_form; } #
# Title:
# Artist: # ... #
=head1 DESCRIPTION This module helps to generate HTML forms for creating new database rows or editing existing rows. It maps column names in a database table to HTML form elements which fit the schema. Large text fields are turned into textareas, and fields with a has-a relationship to other C tables are turned into select drop-downs by default to select a row from the joined class. Also enum and boolean columns are made into select boxes. =head1 METHODS The module is a mix-in which adds two additional methods to your C-derived class. =head2 to_cgi This returns a hash mapping all the column names of the class to HTML::Element objects representing form widgets. =cut sub to_cgi { my $class = shift; map { $_ => $class->to_field($_) } $class->columns; } =head2 to_field($field [, $how]) This maps an individual column to a form element. The C argument can be used to force the field type into one of C, C