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

Thomas Klausner domm at cpan.org
Fri Jan 27 17:50:03 CET 2006


Hi!

On Fri, Jan 27, 2006 at 10:02:33AM -0500, Todd Charron wrote:
 
> 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.  

I'm currently also struggeling with HTML::Widget, DBIx::Class and
Catalyst.

Here's what I came up with:

In each controller, I have a method that generates a widget, eg:

sub widget_register {    # create the 'register' widget
  my ($self,$c)=@_;
  my $w=$c->widget('register')->method('get')->action('/users/do_register');
  $w->element('Textfield','email')->label('Email');
  $w->element('Textfield','name')->label('Name')->value('test');
  ... # more fields, constraints etc	
  $w->indicator('email');    # field to check if theres data
  $w->element('Submit')->value('register!');
}


When I need a form, I do: (in My::C::Users)
sub register : Local {
  my ($self,$c)=@_;
  $self->widget_register($c);
}
	
Through some magic, the template 'templates/users/register' is rendered,
which looks like:

[% c.widget('register').process.as_xml %]

I can now get a nice form by requesting /users/register


Now for form submission:
sub do_register : Local {
    my ($self,$c)=@_;

    my $w=$self->widget_register($c);   # get the widget
    my $result = $w->process($c->req);  # pass the request to the widget
	    
    if ($result->has_errors) {
       # error!
    } else {
       # create object
    }
}

This is working quite fine, but I have to manually create a DBIC-object,
because DBIx::Class::WebForm cannot handle HTML::Widget objects


The real problems (currently) start when you want to load DBIC-objects
into HTML::Widget. There's currently no code on CPAN that does this.
Here's a very crude workaround that probably only works for text(area)
fields:
 foreach my $element ( @{ $w->{_elements} } ) {
   my $name=$element->name;
   next unless $item->can($name);
   $element->value($item->$name);
 }
			     
I'm currently thinking of a design for something like
DBIx::Class::WebForm or Class::DBI::FromCGI that works with HTML:Widget
and DBIx::Class.

If other people are interested, let me know. I'll probably post some
design ponderings soon here and on the DBIx::Class list.


-- 
#!/usr/bin/perl                               http://domm.zsi.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}



More information about the Catalyst mailing list