DBIx::Class::FormTools 0.000004 review
DownloadDBIx::Class::FormTools is a utility module for building forms with multiple related DBIx::Class objects. SYNOPSIS In the exampl
|
|
DBIx::Class::FormTools is a utility module for building forms with multiple related DBIx::Class objects.
SYNOPSIS
In the examples I use 3 objects, a Film, an Actor and a Role. Role is a many to many relation between Film and Actor.
package MySchema;
use base 'DBIx::Class::Schema';
__PACKAGE__->load_classes(qw[
Film
Actor
Role
]);
package MySchema::Film;
__PACKAGE__->table('films');
__PACKAGE__->add_columns(qw[
id
title
]);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many(roles => 'MySchema::Role', 'film_id');
package MySchema::Actor;
__PACKAGE__->table('films');
__PACKAGE__->add_columns(qw[
id
name
]);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many(roles => 'MySchema::Role', 'actor_id');
package MySchema::Role;
__PACKAGE__->table('roles');
__PACKAGE__->add_columns(qw[
film_id
actor_id
]);
__PACKAGE__->set_primary_key(qw[
film_id
actor_id
]);
__PACKAGE__->belongs_to(film_id => 'MySchema::Film');
__PACKAGE__->belongs_to(actor_id => 'MySchema::Actor');
In your Model class
use base qw/DBIx::Class/;
__PACKAGE__->load_components(qw/PK::Auto::Pg Core FormTools/);
In your view - HTML::Mason example
< %init >
my $film = $schema->resultset('Film')->find(42);
my $actor = $schema->resultset('Actor')->find(24);
< /%init >
< form >
< input
name="< % $film->form_fieldname('title', 'o1') => 'Title' % >"
type="text"
value="< % $film->title % >"
/ >
< input
name="< % $film->form_fieldname('length', 'o1') % >"
type="text"
value="< % $film->length % >"
/ >
< input
name="< % $film->form_fieldname('comment', 'o1') % >"
type="text"
value="< % $film->comment % >"
/ >
< input
name="< % $actor->form_fieldname('name', 'o2') % >"
type="text"
value="< % $actor->name % >"
/ >
form_fieldname(undef, 'o3', {
film_id => 'o1',
actor_id => 'o2'
}) %>"
type="hidden"
value="dummy"
/ >
In your controller (or cool helper module, used in your controller)
my @objects = DBIx::Class::FormTools->formdata_to_objects($querystring);
foreach my $object ( @objects ) {
# Assert and Manupulate $object as you like
$object->insert_or_update;
}
Requirements:
Perl
DBIx::Class::FormTools 0.000004 search tags