1 namespace System.Web.Mvc {
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.ComponentModel.DataAnnotations;
6 using System.Globalization;
8 using System.Web.Mvc.Resources;
10 public abstract class AssociatedValidatorProvider : ModelValidatorProvider {
11 protected virtual ICustomTypeDescriptor GetTypeDescriptor(Type type) {
12 return TypeDescriptorHelper.Get(type);
15 public override sealed IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context) {
16 if (metadata == null) {
17 throw new ArgumentNullException("metadata");
19 if (context == null) {
20 throw new ArgumentNullException("context");
23 if (metadata.ContainerType != null && !String.IsNullOrEmpty(metadata.PropertyName)) {
24 return GetValidatorsForProperty(metadata, context);
27 return GetValidatorsForType(metadata, context);
30 protected abstract IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes);
32 private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, ControllerContext context) {
33 ICustomTypeDescriptor typeDescriptor = GetTypeDescriptor(metadata.ContainerType);
34 PropertyDescriptor property = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);
35 if (property == null) {
36 throw new ArgumentException(
38 CultureInfo.CurrentCulture,
39 MvcResources.Common_PropertyNotFound,
40 metadata.ContainerType.FullName, metadata.PropertyName),
44 return GetValidators(metadata, context, property.Attributes.OfType<Attribute>());
47 private IEnumerable<ModelValidator> GetValidatorsForType(ModelMetadata metadata, ControllerContext context) {
48 return GetValidators(metadata, context, GetTypeDescriptor(metadata.ModelType).GetAttributes().Cast<Attribute>());