[Catalyst] Question on parameter passing

Matt Pitts mpitts at a3its.com
Mon Feb 18 13:05:07 GMT 2008


> -----Original Message-----
> From: Peter Sørensen [mailto:maspsr at sdu.dk]
> Sent: Monday, February 18, 2008 6:18 AM
> To: The elegant MVC web framework
> Subject: [Catalyst] Question on parameter passing
> 
> Hi,
> 
> Using Catalyst, TT and FormBuilder
> 
> I have a question concerning parameter passing.
> 
> I have an action lists/editlist
> 
> which display a form based on a TT template. In this form the user can
> modify several field and hit the SUBMIT button to update theese fields.
> 
> sub editlist : Local Form {
>         my ($self, $c) = @_;
>         my $data = $c->req->params;
>         my $form = $self->form->new(debug => 0);
> 
>         my $listmembers = $c->model('CatalystModelName::ListsAlias')
>                                         ->find({'listname' => $data-
> >{listname} } );
> 
>         # populate the fields
>         $form->field(name => 'date', value => $listmembers->date);
>         $form->field(name => 'reason', value => $listmembers->reason);
>         $form->field(name => 'by', value => $listmembers->created_by);
>         $form->field(name => 'listname', value => $data->{listname});
> 
>         # Make information available for the template
>         my @membersarray = split(" ",$listmembers->destination);
>         my @popdesc = $listmembers->population_desc;
> 
>         $c->stash->{listname} = $data->{listname};
>         $c->stash->{members} = \@membersarray;
>         $c->stash->{popdesc} = \@popdesc;
>         $c->stash->{created_by} = $listmembers->created_by;
>         $c->stash->{template} = 'editlist.tt2';
> }
> 
> 
> 
> 
> 
> The SUBMIT call a new action lists/savemembers where the update
> according to a database is done.
> 
> 
> The savemembers don't output anything just processing data. I would
> like
> it to stay in the same form after data are processed.
> 
> I have tried several ways make the nessacary information available to
> this:
> 
> 	use the stash ( does'nt work and is probably not the right way to
> deal with this. Tried to
>                       pass the listname. Thought that this information
> would be available to another
>                       process in the controller)
> 
>       use hidden form fields: works but there must be a cleaner way to
> do this.
> 
> 
> How do I deal with this in a proper way using catalyst? And how do I
> process the data and
> leave the form unchanged?
> 
> 
> Regards and thanks
> 
> Peter Sorensen/University of Southern Denmark/mail: maspsr at sdu.dk
> 
> 
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-
> archive.com/catalyst at lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/

The "proper" way for most of what I've seen is to have a form POST to the same URL as its GET and then have some logic in the action to respond differently based on whether the request was a GET or POST. Something like...

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

  my $form = $self->formbuilder;

  $c->forward( 'editlist_form_setup', [ $form ] );

  if ( $c->req->method eq 'POST' ) {  # or $form->is_submitted if applicable
    if ( $form->validate ) {
      $c->forward( 'editlist_update_data', [ $form ] );
    }
    else {
      #... stash some error message to your liking...
    }
  }
}

If you setup FormBuilder correctly, then it will automatically keep the field values and such through the POST request. Additionally, if the form doesn't validate this makes it easy for FormBuilder to display its per-field error messages.

Bottom line - form POSTs should be made to the same URL as the form GET.

v/r

-matt pitts




More information about the Catalyst mailing list