[Catalyst] Using C::C::FormBuilder With DBIC

RA Jones ra.jones at dpw.clara.co.uk
Sun Feb 18 10:21:26 GMT 2007


hkclark at gmail.com wrote:
> Hi Everyone,
> 
> I have been experimenting with Catalyst::Controller::FormBuilder.  I'm 
> trying to provide an edit form for objects read from and written to the 
> DB via DBIx::Class.  Have FormBuilder automatically load the data from 
> DBIC into the form seems pretty easy with something like:
> 
>     my $widget = $c->model('MyAppDB::Widget')->find($id);
>     my $form = $self->formbuilder;
>     $form->values($widget->get_columns);
> 
> 
> However, an elegant way of writing the data back out is not jumping out 
> at me.  I know I could manually copy each field out of the $form object 
> to my $widget object, but it seems like there should be simple way to do 
> it all in one line like I got with the 
> "$form->values($widget->get_columns);" above.
> 
> Any suggestions?  What are other folks doing for C::C::FormBuilder and DBIC?

I'm pretty new to this, but this is what I did:

sub edit : Local Form {

   # get data into form pretty much as above, then:

if ( $form->submitted && $form->validate ) {
   $c->stash->{user} = $user;
   $c->forward('do_edit');
}

sub do_edit : Private {
   my ($self, $c) = @_;
   my $form = $self->formbuilder;
   my $fields = $form->field;
   $c->stash->{user}->update($fields);
   $c->flash->{status_msg} = 'User updated';
   $c->response->redirect( $c->req->base . 'users/view/' .
     $c->stash->{user}->id );
}

Not sure if it's the 'best' way, but it works. Will be interesting to 
see what other suggestions you get.
-- 
Richard Jones
Leeds, UK
mailto:ra.jones at dpw.clara.co.uk



More information about the Catalyst mailing list