Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model-level validation

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.

like image 973
Omar Avatar asked Nov 06 '22 17:11

Omar


1 Answers

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.

like image 188
ntombela Avatar answered Nov 15 '22 11:11

ntombela