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

Carl Franks fireartist at gmail.com
Thu Jul 3 09:05:25 BST 2008


2008/7/3  <kakimoto at tpg.com.au>:
>
> hello all :)
>
>  i;m trying to build a system with catalyst. Followed the tutes and
> used html::FormFu. Works great but fails when i have multiple step
> operations.
>
>  been introduced to Catalyst-Controller-FormBuilder-MultiForm and will
> look at it.
>
> Any more recommendations to achieve the purpose below?
>
> step 1: generate a form prompting users to choose "loan" type (ie. home,
> personal, corporate)
> step 2: generate a form based on the type of loan chosen from step 2
> step 3: save loan type to "loans" table (attributes from form in step
> 1), get ID ,
>           save the attributes from the form in step 2
>
> Using Html::FormFu,
> in step 2, i have loaded the specific YAML config based on the loan type
> by instantiating a new html::Formfu object.
>
> problem is, in step 3, by using $c->{stash}->{form}, i cannot get any
> attributes found in step3.

I'm guessing that in step 3, your $c->stash->{form} is the one created
by the FormConfig action - in which case it's the same form used for
step 1.
It won't validate the submitted parameters, because it doesn't know
about any of the fields you generated in step 2.
What you need to do is again generate the same form as step 2, and use
that to validate the submitted parameters.
Something like this:


sub form1 : Path : FormConfig {
    my ( $self, $c ) = @_;

    my $form = $self->stash->{form};

    if ( $form->submitted_and_valid ) {
        $c->stash->{loan_type} = $form->param_value('loan_type');

        $c->detach('form2');
    }
}

sub form2 : Path : FormMethod('_build_form2') {
    my ( $self, $c ) = @_;

    my $form = $self->stash->{form};

    if ( $form->submitted_and_valid ) {
        # save to database
    }
    elsif ( $c->stash->{loan_type} ) {
        # save loan-type value to hidden field
        $form->get_field('load_type')->default( $c->stash->{loan_type} );
    }
}

sub _build_form2 {
    my ( $self, $c ) = @_;

    # return hash-ref based on $c->req->params->{loan_type}
    # automatically gets passed to $form->populate
}

Although I haven't yet thought about how to handle auto-generated
forms, there's also HTML-FormFu-MultiForm and the appropriate
MultiForm* catalyst actions. These are all still in svn, but do work.

Carl



More information about the Catalyst mailing list