package Class::DBI::AsForm; use 5.006; our $VERSION = '2.42'; use strict; use warnings; use base 'Exporter'; use Class::DBI::Plugin::Type (); use HTML::Element; our $OLD_STYLE = 0; our @EXPORT = qw( to_cgi to_field _to_textarea _to_textfield _to_select type_of ); =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 populated with objects from the joined class. =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