I started a thread on Hanselforum regarding MVC and MVP validation.  Scott responded and I've added another post.  We're discussing where validation should be done in the MVC framework.  Here's what I wrote up as my latest post:


It seems like the Model should "own" validation, but for usability, the View must be able to apply it. Ultimately, I think validation needs to be applied at the time of entry as well as at the time of persistence. I don't like self-servicing models (that can persist themselves), and I like being able to validate models in different ways, depending on how they are being used, so that puts validation in the Controller, but that doesn't seem quite right all of the time either.

Maybe something like this would work...

  1. Model defines core validations like non-nullable fields, validation that is required for data integrity;
  2. The Controller defines augmented validations like scenario-based rules (it's okay to leave field X blank in this scenario, but not others);
  3. All validations being applied to a scenario are passed to the View for UI integration;
  4. The controller enforces all validations before attempting to persist the model;
  5. The model validates itself against the core validations during persistence.

The problem with something like this is that it hinders the efforts of SoC. Every layer becomes involved in validation to some extent. But maybe that's okay so long as it's built into a framework and a set of custom controls.

Validation seems to me to be one of the biggest gaps in the MVC pattern in general, including the ASP.NET MVC framework. Peter Blum was asked whether he'd supply versions of his controls to work in MVC, and he echoed this feeling. When faced with losing controls like Peter's Date Picker, I really hesitate to implement MVC. Users love rich data entry controls, and if we try to tell them that they've lost them because we've moved to a new development framework, they will just be upset. As was discussed, shiny new rocks aren't always good.

I've thought about joining the MvcContrib project and trying to get my framework added in as an option; but I need to get more familiar with ASP.NET MVC as it is out of the box first, before I start adding new options to it.


In case you’re wondering about scenarios where validation would need to be applied differently for the same model… the easiest example I can think of is search forms.  Imagine an employee search form with several optional search criteria.  Employee Numbers must be formatted a certain way.  When performing this search, you’d want to enforce the employee number format to aide the user in the search, but you wouldn’t want to require the field.  But when you’re editing an employee, you need to both enforce the format and make the field required. <p>Situations like this prevent you from defining all validations in the model, and the controller must get involved.</p>