[Maypole-dev] do_edit creation problems

From: Dave Howorth (Dave.Howorth at acm.org)
Date: Fri Dec 31 2004 - 17:40:16 GMT


I'm having trouble when creating a new entry and problems seem to have
arisen in Maypole::Model::CDBI->do_edit somewhere between 1.7 and 2.06.
Relevant code extracts are below.

I'm trying to create a column with an untaint type of date in a slightly
modified BeerDB. Due to stupidity or too much Christmas cheer, I haven't
installed CGI::Untaint::date. It should die but instead users see a
malformed empty form (or a blank screen with a validating browser).

Under 1.7, CGI::Untaint dies when it can't find the module, so there's a
log trace and the user sees a 500 Server Error, which I think is correct
in this case.

Under 2.06, M-M-CDBI traps the die with an eval and puts the message in
an error hash intended for the template. I don't understand this because
(a) there's no log message so I can't find out what happened to debug it
and (b) my users would see a Perl error message.

The problem is compounded because of another change between 1.7 and
2.06, because instead of displaying the edit template, which shows
column-specific errors at least (though not FATAL ones) it now displays
the addnew template. But addnew is not a top-level template, it's a
component that's intended for use in list so the result in isolation
isn't properly formed HTML. Further, it's not designed for this use and
has no error-processing capability.

I guess this latter issue was in response to bug #7917 - "if do_edit
fails for object creation, try again with the correct template (addnew)"
- but I think the suggested fix is wrong.

One possible solution might be another template like 'edit', called
perhaps 'create' that populates the fields from the params instead of
the object. If that's what's wanted, I'm happy to put one together.

Cheers, Dave
===============================================================

In 1.7:
     if ($obj) {
         # We have something to edit
         $obj->update_from_cgi($h => {
            required => $r->{config}{$r->{table}}{required_cols} || [],
                                     });
     } else {
         $obj = $self->create_from_cgi($h => {
            required => $r->{config}{$r->{table}}{required_cols} || [],
                                     });
         $creating++;
     }
     if (my %errors = $obj->cgi_update_errors) {
         # Set it up as it was:
         $r->{template_args}{cgi_params} = $r->{params};
         $r->{template_args}{errors} = \%errors;
         $r->{template} = "edit";
         undef $obj if $creating; # Couldn't create
     } else {
         $r->{template} = "view";
     }
     $r->objects([ $obj ]);

===============================================================

In 2.06
     if ($obj) {
         # We have something to edit
         eval {
             $obj->update_from_cgi( $h =>
                 { required => $r->{config}{ $r->{table}
}{required_cols} || [], }
             );
         };
         $fatal = $@;
     }
     else {
         eval {
             $obj =
                 $self->create_from_cgi( $h =>
                     { required => $r->{config}{ $r->{table}
}{required_cols} || [], }
             );
         };
         $fatal = $@;
         $creating++;
     }
     if ( my %errors = $fatal ? (FATAL => $fatal) :
$obj->cgi_update_errors ) {

         # Set it up as it was:
         $r->{template_args}{cgi_params} = $r->{params};
         $r->{template_args}{errors} = \%errors;

         if ($creating) {
             undef $obj;
             $r->template("addnew");
         } else {
             $r->template("edit");
         }
     }
     else {
         $r->{template} = "view";
     }
     $r->objects( [$obj] );

===============================================================

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.298 / Virus Database: 265.6.7 - Release Date: 30/12/04

_______________________________________________ maypole-dev mailing list maypole-dev at lists.netthink.co.uk http://lists.netthink.co.uk/listinfo/maypole-dev



This archive was generated by hypermail 2.1.3 : Thu Feb 24 2005 - 22:25:57 GMT