[Catalyst] Where should constraints go

Rodney Broom rbroom+catalyst at rbroom.com
Fri Nov 3 17:02:27 GMT 2006


From: "Ian Docherty" <ian at iandocherty.com>

> Putting these sort of constraints is the duty of either the database 
> level (where they naturally go anyway) or perhaps in the business logic 
> layer.

Right, constraints can come from both. I think the question is how to communicate those contraints up the line, ultimately to the view. Which is what you said below. :)


> My problem is finding a clean way of getting these constraints out of 
> the Model and into the View so that I can generate meaningful error 
> messages without hard-coding them in the templates.

I don't have the communication from layer to layer all worked out, but an approach I've liked is to have errors (like constraint failures) communicated with standard message identifiers and know data. For instance, failing length of 'username' could propogate a message ID of ERR_USERNAME_LENGTH, and include data in a list: [1, 16]. From there, the view would be able to pull messaging based on language or other criteria and flesh it out with the data. In TT, maybe something like:

    [% IF field.messages %]
      [% FOR m = field.messages %]
        [% sprintf( get_text(m.id), @{m.data} ) %]
      [% END %]
    [% END %]

The English message for ERR_USERNAME_LENGTH might be:

  Must be between %u and %u characters



> Perhaps I am expecting too much.

I don't think so. I've seen this done pretty well, but am finding it to be a labor to get it all worked out under Catalyst.


btw, this general path isn't mine. I got it years ago, pre-catalyst, from another Cat-list person: Mark Blythe.


---
Rodney Broom




More information about the Catalyst mailing list