[Catalyst] Any recommendations for multiple forms in catalyst (have been using HTML::FormFu)?

Bill Moseley moseley at hank.org
Tue Jul 15 20:03:36 BST 2008


On Tue, Jul 15, 2008 at 11:13:34PM +1000, kakimoto at tpg.com.au wrote:
> hi 
>  I was pretty successful in keeping things simple.
> 
> 
> sub create :Local  :FormMethod('_get_dynamic_form') { 
>     my ($self, $c) = @_;
> 
>     # Set the template
>     my $effective_template = 'listings/create.tt2';
>     $c->stash->{template} = $effective_template;
>     my $loanType = lc($c->request->param('loanType'));
> 
>     my $step = $c->flash->{step};
>     $c->log->debug("Current operation: $step");
> 
>     if (defined($step) and $step =~ m/\w+/)
>     {
>         my $form_submission_success = $c->stash->{'form'}->submitted();
>         if ($form_submission_success) {
> 
>             if ($step eq 'first_step')
>             {
>                ....
> 
> 
>              # now, set the next step.
>               $c->flash->{'step'} = 'second_step';
>            } 
>            elsif ($step eq 'second_step')
>            {
>              ....
> 
>   
>              # now, set the next step.
>               $c->flash->{'step'} = 'third_step';
> 
>            }
>            elsif ($step eq 'third_step')
>            {
>              ....
> 
>   
>              # now, we have acquired all data we need from steps one and
> two.
>              # we do a redirect/forward/detach to save the values. 
> Should go to 'sub save_complete_listing'
> 
>              $c->detach/ $c->res->forward/$c->redirect   # all failed.
>            }


Have not been following that closely, but that looks complicated.
I would do something like this with Form::Processor:

    sub first_step : Local {
        my ( $self, $c ) = @_;

        return $c->post_redirect( 'second_step' )
            if $c->update_from_form;
    }


    sub second_step : Local {
        my ( $self, $c ) = @_;

        return $c->redirect( 'first_step' )
            unless $c->session->{first_step};

        return $c->post_redirect( 'third_step' )
            if $c->update_from_form;

    }


    sub third_step : Local {
        my ( $self, $c ) = @_;

        return $c->redirect( 'second_step' )
            unless $c->session->{second_step};

        return $c->post_redirect( '/home', 'Thanks for your order' )
            if $c->update_from_form:
    }

My forms are classes, thus each form class knows to save their data in the
session (except third_step which writes all the form data).  They also
know how to pre-populate the form from a previous submission (for
example, if someone goes from form three back to form one.

update_from_form() knows the form class from the action name, and
$c->redirect and $c->post_redirect are simple short-cut methods to
build the redirect and localize any messages that end up in flash.


-- 
Bill Moseley
moseley at hank.org
Sent from my iMutt




More information about the Catalyst mailing list