[cgiapp] Unexpected variable persistence with CAD / CADS
Richard Jones
ra.jones at dpw.clara.co.uk
Fri Oct 17 11:00:07 EDT 2008
Using CGI::Application::Dispatch with CGI::Application::Dispatch::Server
Editing a record with: <form method="post" action="/foo/update/2">
# form contains record #2 data
CAD table is configured as :app/:rm/:id, so the request is handled by
Foo::update
###################
package Foo;
sub update {
my $self = shift;
my $vars = $self->query->Vars;
if ( my $id = $self->param('id') ) {
$vars->{id} = $id; # should only contain a value if an update req.
}
# update db with content of $vars;
}
####################
This works as intended - updates record #2. But if my next action is to
request a form to create a new record - eg http://localhost:8080/foo -
<form method="post" action="/foo/new_record">
# form populated with new data unrelated to previous edit
on submission of the form, instead of getting a new record created I get
an update of an existing record, which gets clobbered with the new
incoming data. That's because $vars{id} (generated in same way as above)
contains the value of the previous action id.
With CAD debugging switched on, for an unrelated url request, for
example http://localhost:8080/bar, console output is:
Final args to pass to new(): $VAR1 = {
'PARAMS' => {
'config_files' => [
'./config/config.pl',
'./config/validation.pl'
],
'id' => '2' ###### <==
}
};
That value of 'id' (2 in this case) persisted from the previous action,
and will continue to live indefinitely across multiple requests until
it is either superceded by another id, or until I re-start the CAD server.
The solution (so far) is to avoid the use of $self->param('id') entirely
and pass the id in as a hidden form field. But that partly defeats the
functionality of the CAD table declaration.
I can't work out why the id param is being retained across requests like
this, whether it's a problem related to using CAD/CAD::Server, or
something I've overlooked in my app or CAD setup. But as it stands it is
potentially lethal as it will sooner or later update the wrong record.
--
Richard Jones
More information about the cgiapp
mailing list