Class-DBI-AsForm-2.42/0000755000175200017520000000000010307413457013043 5ustar tonytonyClass-DBI-AsForm-2.42/t/0000755000175200017520000000000010307413457013306 5ustar tonytonyClass-DBI-AsForm-2.42/t/01.t0000644000175200017520000000272310307413227013712 0ustar tonytonypackage Foo; use Test::More; eval "require DBD::SQLite" or plan skip_all => "Couldn't load DBD::SQLite"; plan tests => 4; package DBI::Test; use base 'Class::DBI'; BEGIN { unlink 'test.db'; }; DBI::Test->set_db("Main", "dbi:SQLite:dbname=test.db"); DBI::Test->db_Main->do("CREATE TABLE foo ( id integer not null primary key, bar integer, baz varchar(255) );"); DBI::Test->db_Main->do("CREATE TABLE bar ( id integer not null primary key, test varchar(255) );"); DBI::Test->table("test"); package Bar; use base 'DBI::Test'; Bar->table("bar"); Bar->columns(All => qw/id test/); Bar->columns(Stringify => qw/test/); sub retrieve_all { bless { test => "Hi", id => 1}, shift; } package Foo; use base 'DBI::Test'; Foo->table("foo"); use_ok("Class::DBI::AsForm"); no warnings 'once'; $Class::DBI::AsForm::OLD_STYLE=1; *type_of = sub { "varchar" }; Foo->columns(All => qw/id bar baz/); like(Foo->to_field("baz"), qr/has_a(bar => Bar); is(Foo->to_field("bar"), "\n", "Select OK"); my $x = bless({id => 1, bar => Bar->retrieve_all(), baz => "Hello there"}, "Foo"); my %cgi = ( id => ' ', bar => ' ', baz => ' ' ); is_deeply({$x->to_cgi}, \%cgi, "All correct as an object method"); Class-DBI-AsForm-2.42/t/pod.t0000644000175200017520000000020110307412534014241 0ustar tonytonyuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Class-DBI-AsForm-2.42/t/pod-coverage.t0000644000175200017520000000024110307412534016036 0ustar tonytonyuse Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); Class-DBI-AsForm-2.42/lib/0000755000175200017520000000000010307413457013611 5ustar tonytonyClass-DBI-AsForm-2.42/lib/Class/0000755000175200017520000000000010307413457014656 5ustar tonytonyClass-DBI-AsForm-2.42/lib/Class/DBI/0000755000175200017520000000000010307413457015254 5ustar tonytonyClass-DBI-AsForm-2.42/lib/Class/DBI/AsForm.pm0000644000175200017520000001030510307413432016771 0ustar tonytonypackage 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