[Catalyst] Form validation in insert() and update()?

mla maurice.aubrey at gmail.com
Mon May 14 22:56:26 GMT 2007


Dave Rolsky wrote:
> On Mon, 14 May 2007, mla wrote:
> 
>> Anyone have validation logic in the model and are happy with it?
> 
> There are two kinds of validation here. One is model-level validation, 
> and yes, it's in my model code. My model throws exceptions, which I trap 
> in the controller and "mess with" to make it work for the web UI.
> 
> The controller might also do some validation, but all it's doing is 
> validating things specific to the controller's environment (in this 
> case, the web UI).
> 
> Putting your model validation in the controller is a horrible violation 
> of the layering that makes MVC work. What do you do when you want to 
> insert/update/delete outside of the web UI?
> 
> The controller is the bridge between a specific environment (web, CLI, 
> REST API) and the model. That means _most_ of your code for an app 
> should probably be in the model. The controller is basically a simple 
> translation layer between the stuff the client provides and the model API.

That's makes total sense to me.

Could you give an example of how you munge the exceptions into error
messages for the user?

For example, say we have a User model with an email field. And we want
to ensure the e-mail format is somewhat sane. So you have something in
your DBIx::Class User model that checks the e-mail value whenever an
attempt is made to modify it. If it's invalid, you raise an exception
of what sort? An object exception or just a string?

And then you need to map that back to a user message. The html form
could be asking for something very specific -- say, your mother's e-mail
address. So you need to convert the generic "e-mail invalid" error into
something like "Your mother's e-mail address is invalid". Something of 
that sort?

Thanks.



More information about the Catalyst mailing list