[Catalyst] HTML::FormHandler and the Catalyst stash

Lukas Thiemeier spamcatcher at thiemeier.net
Wed Jan 16 10:58:55 GMT 2013


On 01/16/2013 11:39 AM, Jacinta wrote:
> G'day folk,
>
> I've been working through the Catalyst tutorial (again) and I decided to
> give HTML::FormHandler a go. It's tutorial at
> https://metacpan.org/module/GSHANK/HTML-FormHandler-0.40017/lib/HTML/FormHandler/Manual/Tutorial.pod
> has been great.
>
> However I am stuck on something I am certain should be easy, but is not
> proving so...
>
> As per the tutorial my edit method looks much like this:
>
>     sub edit : Local {
>     my ( $self, $c, $book_id ) = @_;
>
>     $c->stash( template => 'books/edit.tt2',
>     form => $self->form );
>
>     # Validate and insert/update database
>     return unless $self->form->process( item_id => $book_id,
>     params => $c->req->parameters,
>     schema => $c->model('DB')->schema );
>
>     # Form validated, return to the books list
>     $c->flash->{status_msg} = 'Book saved';
>     $c->res->redirect($c->uri_for('list'));
>     }
>
>
> and that works beautifully. However, I've enhanced my examples as I've
> gone along and instead of going to the list of books, I want to go to my
> review page and print out all of the details (including extra stuff) of
> this specific book I've just added.
>
> So I would like to change the last line to be:
>
>     $c->res->redirect($c->uri_for($self->action_for('review'), [$book_id]));
>
>
> This works perfectly if I'm editing a book, but if I'm creating a book
> for the first time, $book_id is undefined so I pass in an undefined id
> to review. Review seems to deal with that okay:
>
>     sub review :Chained('object') :PathPart('review') :Args(0) {
>     my ($self, $c) = @_;
>
>     my $book = $c->stash->{object};
>
>     ...
>
> until I want to use $book for something.
>
> I have looked through most of the documentation for HTML::FormHandler
> and I can't find anything that actually documents process() (it returns
> 1 on success) and I don't know the Catalyst stash well enough. Is this
> information hiding therein or do I need to create a method to get the
> last insert id?
>
> All help gratefully appreciated.
>
> Jacinta
>
>
> _______________________________________________
> 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@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/

Hi Jacinta,

Assumed that you are using HTML::FormHandler::Model::DBIC, 
HTML::FormHandler will store the new (or updated) item in the forms 
"item" attribute.

You can try this:

  my $new_id = $c->stash->{form}->item->id;
  $c->res->redirect($c->uri_for($self->action_for('review'), [$new_id]));

This code should work, no matter if the item was updated or created. It 
will even work if the items id changes during the update process (which 
should rarely happen, but it will work if it happens)


I hope I could help.
Lukas




More information about the Catalyst mailing list