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

Carl Franks fireartist at gmail.com
Sun Feb 18 13:01:11 GMT 2007


On 18/02/07, RA Jones <ra.jones at dpw.clara.co.uk> wrote:
> 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.

It might be worth looking at the code for DBIx::Class::HTMLWidget for some ideas
http://search.cpan.org/src/ANDREMAR/DBIx-Class-HTMLWidget-0.09/lib/DBIx/Class/HTMLWidget.pm

When you're populating the form from the database, would your
    $form->values($widget->get_columns);
correctly set select menus?

Also, (for all browsers that I'm aware of) no value is sent back to
the server for unchecked radioboxes - so it'd be worth checking
whether the "$fb->field" solution handles this properly.
(I don't know, as I've only read the formbuilder docs, I've never used it).

Cheers,
Carl



More information about the Catalyst mailing list