Can you recursively validate objects with VAB?

Topics: Validation Application Block
Mar 11, 2010 at 4:45 PM
Edited Mar 11, 2010 at 6:41 PM

For examle:

 

public class Name
{
    [Required]
    [StringLength(20)]
    public string FirstName{ get; set; }

    [StringLength(1)]
    public string MiddleName{ get; set; }

    [Required]
    [StringLength(20)]
    public string LastName{ get; set; }
}


public class Person
{
    [Required]
    public Name PersonsName {get;set;}
   
    [Required]
    [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?

Thanks,
Michael

 

 

Mar 12, 2010 at 1: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.
entlib.support@avanade.com