DBIx::Class::HTMLWidget3pm
Like fromform but with dbix::class and html::widget
Usage
You'll need a working DBIx::Class setup and some knowledge of HTML::Widget and Catalyst. If you have no idea what I'm talking about, check the (sparse) docs of those modules.
package My::Model::DBIC::Pet;use base 'DBIx::Class';__PACKAGE__->load_components(qw/HTMLWidget Core/);package My::Controller::Pet; # Catalyst-style# define the widget in a sub (DRY)sub widget_pet {my ($self,$c)=@_;my $w=$c->widget('pet')->method('get');$w->element('Textfield','name')->label('Name');$w->element('Textfield','age')->label('Age');...return $w;}# this renders an edit form with values filled in from the DBsub edit : Local {my ($self,$c,$id)=@_;# get the objectmy $item=$c->model('DBIC::Pet')->find($id);$c->stash->{item}=$item;# get the widgetmy $w=$self->widget_pet($c);$w->action($c->uri_for('do_edit/'.$id));# fill widget with data from DB$item->fill_widget($w);}sub do_edit : Local {my ($self,$c,$id)=@_;# get the object from DBmy $item=$c->model('DBIC::Pet')->find($id);$c->stash->{item}=$item;# get the widgetmy $w=$self->widget_pet($c);$w->action($c->uri_for('do_edit/'.$id));# process the form parametersmy $result = $w->process($c->req);$c->stash->{'result'}=$result;# if there are no errors save the form values to the objectunless ($result->has_errors) {$item->populate_from_widget($result);$c->res->redirect('/users/pet/'.$id);}}
Overview
Something like Class::DBI::FromForm / Class::DBI::FromCGI but using HTML::Widget for form creation and validation and DBIx::Class as a ORM.
Methods
fill_widget
$dbic_object->fill_widget($widget);
Fill the values of a widgets elements with the values of the DBIC object.
populate_from_widget
my $obj=$schema->resultset('pet)->new->populate_from_widget($result);my $item->populate_from_widget($result);
Create or update a DBIx::Class row from a HTML::Widget::Result object
Caevats / possible problems
PostgreSQL
ERROR: null value in column "private" violates not-null constraint
This is a result of we trying to set a value to undef that should not be. This is typically a problem when you have a column such ass "private boolean not null". We have a special-case for this, and if you set data_type => boolean, is_nullable => 0 in your ResultSource definition, we update the value to 0 before attempting to insert or update
Contributors
Simon Elliott, <[email protected]>
Ashley Berlin
Guillermo Sansovic
Releases
9 releases since Debian 5.0 (Lenny); the text changed 4 times.