Michel
Michel

Reputation: 23615

Where do i place my viewmodel validation?

we are building an ASP.Net MVC application, and we ask ourselve the question where we shoud put the validation logic for incoming data.

We already have the simple validation in place: these are attributes on the viewmodel, like [required], [numeric] , [email] etc. (this is open for discussion too, btw)

But now we have some more input validation: we want to validate if the id's received from dropdownlists are genuine id's.

For example: when we receive 91 as a countryid, i have to make sure 91 is a valid countryid and not a value 'hacked into' the form by a user. Because if it is not a valid countryid, my datalayer generates an error.

  1. Should i place this in the controllers action method, because that method knows what is right and what is wrong when the data from the request arrives?

  2. Should i place it in a VacancyValidator (the object is a Vacancy object) where i put all validation logic for all vacancy related viewmodels

  3. Should i place it in the ViewModel because it should know how to validate itself

  4. Should i create an attribute which validates the property which i place on the ViewModels property

  5. Should i place it in a Vacancy[thisviewmodelsname]Validator where i put all validation logic for this specific viewmodel

Any ideas appreciated....

Upvotes: 3

Views: 635

Answers (3)

Display Name
Display Name

Reputation: 4732

You put your validation obviously to the view model. This way it is located in one spot (not scattered, hence DRY) and operational on both client and server.

Hope this helps.

Upvotes: 1

Alexey Bychkov
Alexey Bychkov

Reputation: 581

The best way is to combine client and server validation. For client side validation you can use jquery validation plugin. It provides all standart validation patterns(like maxlength, minlength, required etc.).You can specify validation requirements in model attributes(data annotation) or directly in html. Also, it provides possibility for custom remote validation when validation result will be returned from server code.

But you should dublicate client side validation with server side. For example, use spring.net validation . I think it's the best generic and flexible framework.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

We already have the simple validation in place: these are attributes on the viewmodel, like [required], [numeric] , [email] etc. (this is open for discussion too, btw)

I would recommend FluentValidation.NET instead of Data Annotations which plays nicely with ASP.NET MVC. It provides a nice syntax for expressing complex validation logic between interdependent properties without writing millions of lines of plumbing infrastructure code (which is what you would have to do if you use Data Annotations and write a custom validator) and also allows you to unit test your validation logic very easily.

Because if it is not a valid countryid, my datalayer generates an error.

There you go - your data layer already handles this validation for you. But if you don't want to leave it to reach the data layer then you could have a validation rule for this property on the view model. If you follow my previous advice about FluentValidation.NET you will already know where to put this rule - in the corresponding validator of your view model.

Upvotes: 2

Related Questions