Created
July 31, 2010 23:22
-
-
Save thegrubbsian/502736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ValidateChildAttribute : ValidationAttribute { | |
public override bool IsValid(object value) { | |
if (value == null) return true; | |
var isValid = IsItValid(value); | |
if (!isValid) return false; | |
if (value is IEnumerable) return ((IEnumerable)value).Cast<object>().All(IsItValid); | |
return true; | |
} | |
private static bool IsItValid(object value) { | |
return Validator.TryValidateObject(value, new ValidationContext(value, null, null), | |
new List<ValidationResult>(), true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This attribute allows the System.ComponentModel.DataAnnotations Validator.TryValidateObject method to validate into complex child properties of an object.