Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Update data annotations validation to include class level attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xt0rted committed Mar 2, 2014
1 parent b4e3bb3 commit 9f207dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Expand Up @@ -142,7 +142,7 @@ private class OopsValidationAttribute : ValidationAttribute
{ {
protected override ValidationResult IsValid(object value, ValidationContext validationContext) protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{ {
return new ValidationResult("Oops"); return new ValidationResult("Oops", new[] { string.Empty });
} }
} }


Expand Down
Expand Up @@ -38,7 +38,7 @@ protected DataAnnotationsValidatorAdapter(string ruleType)
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns>
public virtual IEnumerable<ModelValidationRule> GetRules(ValidationAttribute attribute, PropertyDescriptor descriptor) public virtual IEnumerable<ModelValidationRule> GetRules(ValidationAttribute attribute, PropertyDescriptor descriptor)
{ {
yield return new ModelValidationRule(ruleType, attribute.FormatErrorMessage, descriptor == null ? null : new[] { descriptor.Name }); yield return new ModelValidationRule(ruleType, attribute.FormatErrorMessage, new [] { descriptor == null ? string.Empty : descriptor.Name });
} }


/// <summary> /// <summary>
Expand Down
Expand Up @@ -38,6 +38,17 @@ public IEnumerable<IPropertyValidator> GetValidators(Type type)
var results = var results =
new List<IPropertyValidator>(); new List<IPropertyValidator>();


var classAttributes =
typeDescriptor.GetAttributes().OfType<ValidationAttribute>();

var classValidator =
new PropertyValidator
{
AttributeAdaptors = this.GetAttributeAdaptors(classAttributes)
};

results.Add(classValidator);

foreach (PropertyDescriptor descriptor in propertyDescriptors) foreach (PropertyDescriptor descriptor in propertyDescriptors)
{ {
var attributes = var attributes =
Expand Down

0 comments on commit 9f207dd

Please sign in to comment.