Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation: Model or ViewModel

Tags:

Where should validation reside when using ViewModels with MVC (MVVM), on the Model or the ViewModel? (Or both or neither)? And why? I bring this up especially in light of V2 of ASP.NET MVC coming out soon.

What about complex/custom validation?

like image 720
Matthew Groves Avatar asked Aug 10 '09 01:08

Matthew Groves


People also ask

What is difference between ViewModel and model?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

What is meant by model validation?

Model validation refers to the process of confirming that the model actually achieves its intended purpose. In most situations, this will involve confirmation that the model is predictive under the conditions of its intended use.

Should models have validation?

The right place for validation is the Model. This makes most sense because you are doing validation on the data, which is what the model represents. In terms of the CRUD updates, the model should always be used somehow. If you are changing data from the view, you should have validations being checked.


1 Answers

Any business specific validation should be in the Model. Any web site specific validation could be done in both the Model and/or ViewModel. This way your business logic (Models) can move behind a different interface (Windows, Web Forms, etc.) and your validation will stay intact.

like image 166
mkchandler Avatar answered Feb 05 '23 17:02

mkchandler