Skip to content

Instantly share code, notes, and snippets.

@thegrubbsian
Created July 31, 2010 23:22
Show Gist options
  • Save thegrubbsian/502736 to your computer and use it in GitHub Desktop.
Save thegrubbsian/502736 to your computer and use it in GitHub Desktop.
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);
}
}
@thegrubbsian
Copy link
Author

This attribute allows the System.ComponentModel.DataAnnotations Validator.TryValidateObject method to validate into complex child properties of an object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment