Can you recursively validate objects with VAB?

Topics: Validation Application Block
Mar 11, 2010 at 3:45 PM
Edited Mar 11, 2010 at 5:41 PM

For examle:


public class Name
    public string FirstName{ get; set; }

    public string MiddleName{ get; set; }

    public string LastName{ get; set; }

public class Person
    public Name PersonsName {get;set;}
    [Range(18, 100)]
    public short Age {get;set;}

var person1 = new Person();
person.Age = 18

//Give error about LastName required
var results1 = Validation.Validate<Person>(person1);

var person2 = new Person();
var name = Name()
name.FirstName = "John"
name.MiddleName = "sdfsfsdf"

//Everything is valid even though LastName is required
//and MiddleName string length is to long.
var results = Validation.Validate<Person>(person2);

So is there a way to recursively validate all objects in the object graph?




Mar 12, 2010 at 12:36 AM

Yes, decorate the PersonsName property with the [ObjectValidator] attribute.  For list type of property, use an [ObjectCollectionValidator] attribute.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.