[Catalyst] Lightweight error/messages reporting to templates

Michael Reece mreece at vinq.com
Wed May 23 19:58:50 GMT 2007


i don't know about 'best practice' but what i have done is create a  
plugin that exposes $c->alert_error(), $c->alert_info(), and a couple  
of similar ones.

the controller does, roughly:

   $c->alert_error('You screwed up.'); # moreorless push @{ $c->flash- 
 >{alert_errors} }, @_;
   $c->alert_info($obj->name . ' updated!');

these are stored in the flash (for redirect-after-successful-post).

i then populate stash/globals with the flash contents before  
rendering the view (mason in my case).

my mason autohandler then displays any @alert_errors/etc as appropriate.

i use the same system for $c->form_error(username => 'Username  
missing.'), which is deleted into %form_errors before rendering the  
view.  my custom form generators (mason components) know how to  
display those errors as appropriate when the form is rendered.



On May 23, 2007, at 11:29 AM, Paul Makepeace wrote:

> I'm hoping there's a 'best practice' or common pattern for reporting
> errors and messages to templates (not necessarily just forms).
>
> I find myself doing stuff like,
>
> my @errors; my @messages;
> # ...
> if ($some_err_condition) {
>   push @errors, "whoops";
> } else {
>   $obj->update;
>   push @messages, $obj->name . " updated!";
> }
> # ...
> $c->stash(messages => join "<br/>", @messages);
> $c->stash(errors => join "<br/>", @errors);
>
> Which is then tediously copy & pasted amongst controllers that do that
> reporting.
>
> And then my template says something like,
>
>  [% IF errors %]
>    <p class="error">[% errors %]</p>
>  [% END %]
>  [% IF messages %]
>    <p class="messages">[% messages %]</p>
>  [% END %]
>
> At least with the template I have that as a MACRO I can call variously
> but I'd at least like a way to have some kind of
> context/controller-wide way of dealing with these errors & messages. I
> don't quite understand Cat's object model well enough to do this
> globally myself yet.
>
> P
>
> _______________________________________________
> List: Catalyst at lists.rawmode.org
> Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/ 
> catalyst at lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/

---
michael reece :: software engineer :: mreece at vinq.com





More information about the Catalyst mailing list