Why you should validate in your controllers and not your models
Okay, I got some responses based on my question yesterday about why validation shouldn’t be in the model of an MVC-based app.
This is what I got out of the responses:
🔗 Models don’t know about the current user (or other higher level information)
This means that if you have some kind of time based input the timezone modifications need to happen in the controller. Or the even better example is that sometimes a user can change more of a model than another user based on the user’s permissions or roles. Personally I would just have a method on the model that had specific columns enabled and disabled, and then it would throw exceptions based on what columns the user tried to set. I can’t say for sure whether or not that is a scalable methodology. But it seems reasonable.
🔗 The earlier you validate the better, or fail fast
General programming practices often say that you should fail fast. This makes sense because you then have less time for bad data to be in the system. The problem is that assumes that you validate your data in the controller. If your model does validation, (again, assuming you validate at all) you should never be able to have bad models. But if you create some model at more than one place in your Controller, then you have to validate in both. I personally don’t think that this is a good argument.
🔗 If you do decide to do model based validation you need a structure to deal with that
Basically the issue here is that models shouldn’t return error messages that the user reads. The reason being that the model doesn’t know what language the user speaks, or in what context the user will read the message, and therefore, can’t give the user the correct language error message. So instead the controller gets some kind of error code from the model and translates that into an error message. Not really a hard thing to do, but certainly something to keep in mind.
🔗 Do all validation in the database
I have decided that I am going to try to integration my validation with my models. I know there are places where I will have to do validation in the controller, and that I also need to have the controller relay the messages from the model to the user, but it still seems to me that the controller doing validation should be the exception rather than the rule.Posted Thu, Jun 18, 2009
If you're interested in being notified when new posts are published, you can subscribe here; you'll get an email once a week at the most.