[Catalyst] Form processing with catalyst, html::widget, and template toolkit

Zbigniew Lukasiak zzbbyy at gmail.com
Fri Jan 27 20:16:05 CET 2006

Here is how I use widgets in Catalyst::Example::InstantCRUD (you can
install that module and use the provided example generator to create
your own CRUD application using widgets).

It is probably a bit simplistic when compared to the other method
supplied here.  But I hope it has some good points.

First I have a function to build the widget with all the needed fields
(needed for CRUD) - _build_widget.  It fills the values in the widget
from the database.

Then I use that function to create the widget and put it on stash:

my $item = $model_class->find($id);
my $w = $self->_build_widget($c, $id, $item)->process();
 $c->stash->{widget} = $w;

And I use that in the template to display it ([% widget %]).

I have separate controllers for the first round, when the form gets
the values from the database object, that was the above, and for the
second round if there are any errors, when I fill the widget with
values from the request and check the errors:

 my $result = $self->_build_widget($c, $id)->process($c->request); #
process refills widget values from request, and marks errors

        $c->stash->{widget} = $result;
        $c->stash->{template} = 'edit.tt';
        $c->form( optional => [ $model_class->columns ] );
        $model_class->find($id)->update_from_form( $c->form );

Here you can see that I use WebForm to upudate the DBIC object.  I
check the values with HTML::Widget, but than I use the other mechanism
to update the object, that works for me.

At the display you need to manipulate the CSS to have the errors well
formated, you can check InstantCRUD to see how I do that, but I am not
proud of it.

I know that code needs some refactoring.  Thanks for reminding me that.

-- Zbyszek

On 1/27/06, Todd Charron <todd at neteffect.ca> wrote:
> Hi,
> I'm just starting with catalyst and I'm trying to get my head around how all
> of these modules work together/how they're supposed to be best used.
> Previously I've been doing sites with CGI Formbuilder and HTML Template, so
> I'm fairly new with catalyst, html::widget and template toolkit.
> My question is, when using html::widget how does that fit into the MVC model?
> Does all of it go in the controller?  If so, how is it included in a TT view?
> If you're just using html::widget for form validation, how do you get from
> form display, to validation, back to the form (or to the completion of the
> form)?
> If you're creating the form with html::widget how do you get it into the view
> from the controller and back again?
> It's probably really obvious, but for some reason I just can't seem to wrap my
> head around it.  Any help would be greatly appreciated.
> Thanks
> Todd
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst

More information about the Catalyst mailing list