[Catalyst] Where should constraints go

Bill Moseley moseley at hank.org
Fri Nov 3 15:23:05 GMT 2006


On Fri, Nov 03, 2006 at 02:40:13PM +0000, Ian Docherty wrote:
> This seems to make sense to me since it is the Model that knows the 
> constraints of the database. If I were to do other (business logic) type 
> constraints then I would probably put in a business logic layer where 
> these constraints were defined.

I do it in both places.  If the username is unique then it's
constrained that way in the database.

I don't use FormValidator but I do have a form object that validates
parameters -- so it would also check if the username was unique (even
though it can't know for sure until it tries to do the insert).  It
would then set an error message on that field "name is not unique in
the database", for example.

> The problem is in the View. To give a text error message that 'Must be 
> between 1 and 16 characters' is hard-coding a constraint directly in the 
> view which would need to be changed if the database schema changed.

I would think your form processing would detect that the username is,
say, grater than 16 chars and set the error message.  Then your view
template would only need to display the error set in the form object
instead of being concerned with what the specific error message is.

I'm not sure where internationalization of the error message would
take place, though.  I might be tempted to pass the language into the
form object.

I'm never clear where forms go in the MVC structure.  In my App
directory I have:

    $ls -1
    Controller
    Form
    Menu
    Model
    View

-- 
Bill Moseley
moseley at hank.org




More information about the Catalyst mailing list