views:

452

answers:

5

Does ASP.NET MVC provide a standard validator functionality or do you have to create your own validation manually? If the latter, is there any third party validator available that you can use on ASP.NET MVC web applications?

+4  A: 

Shortly after I posted this answer I found xval which is a validation framework for ASP.NET MVC.

Spoike
I started using this the other day and it was a snap to get working. The best part was it only required a single line in my views to get the jquery validation working client side. Good stuff.
Todd Smith
+1  A: 

David Hayden wrote an article over at www.codebetter.com describing a great way to handle validation. Of course xVal is an option but it's always great to have an understanding.

Chad Moran
+3  A: 

ASP.NET MVC contains methods like Html.ValidationSummary() and Html.ValidationMessage(). These are updated automatically if you use TryUpdateModel. You could also validate manually and set the errormessages yourself. Here is an example of how use it.

georg
It also works with DefaultModelBinder, that is simply accepting a complex type as a parameter to a controller action.
Tim Scott
+1  A: 

I implemented a variant of the code I found on Stephen Walther's blog. I use it with LINQ2SQL models by defining an IValidatedEntity interface that includes the GetRuleViolations() method and implementing the partial OnValidate method that calls GetRuleViolations() and throws a custom exception if the number of violations is non-zero. In the controller, this fires on SubmitChanges for the data context. If I get an exception I requery the model via the GetRuleViolations() method to build model errors to pass back to the view.

tvanfosson
+1  A: 

You can also checkout the .net validation framework. Its a rules framework that lets you create validators, apply the validators to rules, attach rules to your model, and check those rules at runtime on both the client and server. It provides flexible ways to configure rules - making heavy use of linq for both fluent and strongly typed configuration. It also provides extensibility points to create your own client script generators and rules.

The framework leverages the MVC RC HtmlHelpers and default conventions.

If you download the latest source you can see an example of the framework working in the SplitBranch -> QSAspMvc quickstart project. Its still being actively developed.

TheDeeno