[Catalyst] Emulating Catalyst::Request::Upload for HTML::FormHandler

Amiri Barksdale amiribarksdale at gmail.com
Wed Sep 1 02:35:11 GMT 2010


On Wed, Aug 11, 2010 at 01:08:47PM +0100, Tomas Doran wrote:
| 
| On 10 Aug 2010, at 19:59, Shlomi Fish wrote:
| >I did not get any reply for this on IRC so I'm asking here.
| 
| I believe you did later get a reply on irc.
| 
| Could you share with the rest of the class for archival purposes?

I had a project at work like this recently. I learned a lot from this
wiki entry:

http://wiki.catalystframework.org/wiki/wikicookbook/controllerwithfileupload

If you have a form field like this:

    has_field 'file' => (
        type     => 'Upload',
        max_size => '5242880',
        id       => 'photo_file_input',
        required => 1,
    );

You can process your form like this in the controller, redefining the
file param:
 
    if ( lc $c->req->method eq "post" ) {
        $c->req->params->{file} = $c->req->upload('file');
        my $new_photo =
            $c->stash->{photo_rs}->new_result( { author => $c->user->id } );

        $form->process(
            item   => $new_photo,
            params => $c->req->params,
        );
        $c->stash( fillinform => $form->fif );
        return unless $form->validated;
        $c->res->redirect( $c->uri_for("/photos/gallery") );
    }

But then you have a Catalyst::Request::Upload object in your form field.
So, you have to switch it back after file manipulation, but before
saving (update_model) in the validate method:

    method validate {
        my $upload = $self->field('file')->value;

        $self->process_image( $upload );
        $self->field('file')->value( $self->field('file')->value->filename );
    }

That's one way to do it. We have this running pretty well at work.

Amiri



More information about the Catalyst mailing list