views:

669

answers:

3

How does one accomplish "model-level" validation as stated in Brad Wilson's post:

Finally, if you want a validation to have access to multiple properties, then make it a model-level validation (so that it gets the entire model as the model parameter, rather than a single individual property value).

From http://forums.asp.net/p/1457591/3650720.aspx

I tried to do the following

[MyCustomValidation("SomeStuff")]
public class MyClass
{
     // properties
}

Breakpoints place in the IsValid override of MyCustomValidation trigger nothing, the code just continues. Breakpoints in the constructor of MyCustomValidation work, but nothing after that.

Is this not what model-level validation refers to?

UPDATE:

dirtygopher's link to http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html shows model validation, the only issue is that the validation attributes are placed on the child properties of the parent class.

I'm looking for a way to place validation-attributes directly to the class as I demonstrated in my above example.

+2  A: 

There's a good example for this in the RegisterModel that comes with the latest asp.net mv 2.

Look at the "PropertiesMustMatch" attribute and its usage.

gugulethun
Do you mean in the new project files?
Baddie
Yes. It's in the Models folder.
gugulethun
Using VS 2010 and ASP.NET MVC 2 Preview thats why I didn't see this. Thanks.
Baddie
this example is ok, but would rather prefer a blog post with proper descriptions
Erx_VB.NExT.Coder
A: 

Check out this post of ScottGu :http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx and look at these comments on that : Q:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx#7312052 A1:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx#7312121 A2:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx#7312192 A3:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx#7312204 A4:http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx#7312213

ali62b
These are model validation which are essentially property-level validation. I think `model-validation` refers to putting attributes on the entire class as opposed to the properties of the class.
Baddie
baddie, you didn't read through the q/a's properly... these describe using multiple properties in one validationAttribute ie model/class level validation.
Erx_VB.NExT.Coder
A: 

Here is good post in Brad Wilson's blog http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html. I hope it will help you to understand model-level validation concept.

Gopher
Thank you, this what I was looking for except the article only shows how to place validation on child models of parent models. I.E. I can put attributes on a Model child properties of a the parent model. I was hoping there would be a direct way to place attributes on an entire class not just its properties.
Baddie
hey baddie, i am looking for the same thing. let me know if you find something.
Erx_VB.NExT.Coder