Upload field type

21 views
Skip to first unread message

Steve Rippl

unread,
Apr 24, 2012, 6:56:17 PM4/24/12
to formh...@googlegroups.com
I have a fairly complex form that includes an "around 'update_model'" to do some extra DB stuff.  Everything works as it is until I add an upload field into the mix.  I just want to use FormHandler to validate the file and give nice error messages, I save the actual file elsewhere.  The 'file' field id is not in my DBIC model, however after I do 

$c->req->params->{file} = $c->req->upload('file')
    if $c->req->method eq 'POST';

to get the upload object in there for validation, it breaks within the around 'update_model', specifically at $self->$orig(@_); within the $self->schema->txn_do with the following...

DBIx::Class::Schema::txn_do(): HTML::FormHandler::TraitFor::Model::DBIC::update_model(): first parameter needs to be a hashref at /srv/Site/script/../lib/Site/Form/Node/Edit.pm line 127 at /usr/local/share/perl/5.10.1/HTML/FormHandler/TraitFor/Model/DBIC.pm line 87

I realize I'm being a bit vague, just wondering if this is an obvious error before I start cutting and pasting large chucks of code!

Many thanks,
Steve

Gerda Shank

unread,
Apr 25, 2012, 4:34:49 PM4/25/12
to formh...@googlegroups.com
That message is coming from RecursiveUpdate. The addition of that key to
params is not relevant, that I can see. Probably you're doing something
wrong in your "around 'update_model'".

Are you doing $self->$orig(@_)? Show the code?

Gerda

Steve Rippl

unread,
Apr 25, 2012, 5:09:10 PM4/25/12
to formh...@googlegroups.com
On Wednesday, April 25, 2012 1:34:49 PM UTC-7, GShank wrote:
That message is coming from RecursiveUpdate. The addition of that key to
params is not relevant, that I can see.  Probably you're doing something
wrong in your "around 'update_model'".

Are you doing $self->$orig(@_)? Show the code?


I've included the around update_model below, but if I comment the entire method out and just run a regular update out I still hit that error.  You'll see where I've now added delete($self->params->{file}) because that allowed me to get past the error!  Without that, if I include a file in the form submission it breaks!

Thanks for taking a look!



117 around 'update_model' => sub {
118     my $orig = shift;
119     my $self = shift;
120     my $item = $self->item;
121 
122 # ***HACK***
123 # file uploads seem to break DBIx::Class::ResultSet::RecursiveUpdate here, 
124 # and we save them elsewhere anyway, so now that we've validated we delete 
125 # the file param entry...
126     delete($self->params->{file});
127 
128     $self->schema->txn_do( sub {
129 
130 # run the dbic model updates for node table
131         $self->$orig(@_);
132 
133 # get the access permission fields and update node_access accordingly
134         if ($self->user_roles_ids) {
135             foreach my $role_id ( @{ $self->user_roles_ids } ) {
136                 my $view = $self->field("view-".$role_id)->value || 0;
137                 my $edit = $self->field("edit-".$role_id)->value || 0;
138 
139                 $self->schema->resultset('NodeAccess')->update_or_create({
140                     node_id => $item->id,
141                     role_id => $role_id,
142                     view    => $view,
143                     edit    => $edit,
144                 });
145             }
146         }
147 
148 # update the menu value
149         if ( $self->field('name') && $self->field('name')->value ) {
150             my $menu_item = $self->schema->resultset('Menu')->search({
151                 node_id => $item->id
152             })->first;
153 
154             if ($menu_item) {
155                 if ($self->field('delete_menu_item')->value) {
156                     $menu_item->delete;
157                 }
158                 else {
159                     $menu_item->update({
160                         name        => $self->field('name')->value,
161                         description => $self->field('description')->value,
162                         path        => 'node/'.$item->id,
163                         parent_id   => $self->field('parent_id')->value,
164                         node_id     => $item->id,
165                     });
166                 }
167             }
168             else {
169                 $self->schema->resultset('Menu')->create({
170                     name        => $self->field('name')->value,
171                     description => $self->field('description')->value,
172                     path        => 'node/'.$item->id,
173                     parent_id   => $self->field('parent_id')->value,
174                     node_id     => $item->id,
175                 }) if !$self->field('delete_menu_item')->value;
176             }
177         }
178 
179     });
180 };
 

Steve Rippl

unread,
Apr 25, 2012, 5:52:43 PM4/25/12
to formh...@googlegroups.com
On Wednesday, April 25, 2012 2:09:10 PM UTC-7, Steve Rippl wrote:

I've included the around update_model below, but if I comment the entire method out and just run a regular update out I still hit that error.  You'll see where I've now added delete($self->params->{file}) because that allowed me to get past the error!  Without that, if I include a file in the form submission it breaks!

Thanks for taking a look!


OK, sorry, my head is spinning a bit now!  It's actually delete($self->values->{file}) just before $self->schema->txn_do( sub {  $self->$orig(@_); ... that seems to get me past the error.


Gerda Shank

unread,
Apr 26, 2012, 3:37:20 PM4/26/12
to formh...@googlegroups.com
The error comes from RU checking $form->values that's passed to RU as "updates => $form->values".

Dump $form->values and maybe you can see what's wrong.

The 'params' hash isn't used in update_model, so I can only think that something buried in the 'file' params key is referred to by reference... though how that could make ref( $form->values) eq 'HASH' fail....I can't imagine.

You'll have to do some debugging.

Gerda
Reply all
Reply to author
Forward
0 new messages