Validating DataAnnotations

public static IList<String> GetClassLevelErrors(object instance){    return TypeDescriptor.GetAttributes(instance).OfType<ValidationAttribute>()        .Where(attribute => !attribute.IsValid(instance))        .Select(attribute => attribute.FormatErrorMessage(string.Empty))        .ToList();}

[TestMethod]public void TestMethod1(){    var prop = typeof (DataAnnotationsModelBinderSpike.Models.Contact).GetProperty("First");    var attrib = prop.GetCustomAttributes(true).Cast<RequiredAttribute>().FirstOrDefault();    Assert.IsNotNull(attrib);}

No related posts.

Tags: , , ,

Thursday, November 26th, 2009 ASP.NET MVC, DataAnnotations, ModelBinders, TDD

No comments yet.

Leave a comment

Search