Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/System.Web.Mvc/AssociatedValidatorProvider.cs')
-rw-r--r--src/System.Web.Mvc/AssociatedValidatorProvider.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/System.Web.Mvc/AssociatedValidatorProvider.cs b/src/System.Web.Mvc/AssociatedValidatorProvider.cs
new file mode 100644
index 00000000..b91fade1
--- /dev/null
+++ b/src/System.Web.Mvc/AssociatedValidatorProvider.cs
@@ -0,0 +1,59 @@
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Globalization;
+using System.Linq;
+using System.Web.Mvc.Properties;
+
+namespace System.Web.Mvc
+{
+ public abstract class AssociatedValidatorProvider : ModelValidatorProvider
+ {
+ protected virtual ICustomTypeDescriptor GetTypeDescriptor(Type type)
+ {
+ return TypeDescriptorHelper.Get(type);
+ }
+
+ public sealed override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
+ {
+ if (metadata == null)
+ {
+ throw new ArgumentNullException("metadata");
+ }
+ if (context == null)
+ {
+ throw new ArgumentNullException("context");
+ }
+
+ if (metadata.ContainerType != null && !String.IsNullOrEmpty(metadata.PropertyName))
+ {
+ return GetValidatorsForProperty(metadata, context);
+ }
+
+ return GetValidatorsForType(metadata, context);
+ }
+
+ protected abstract IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes);
+
+ private IEnumerable<ModelValidator> GetValidatorsForProperty(ModelMetadata metadata, ControllerContext context)
+ {
+ ICustomTypeDescriptor typeDescriptor = GetTypeDescriptor(metadata.ContainerType);
+ PropertyDescriptor property = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);
+ if (property == null)
+ {
+ throw new ArgumentException(
+ String.Format(
+ CultureInfo.CurrentCulture,
+ MvcResources.Common_PropertyNotFound,
+ metadata.ContainerType.FullName, metadata.PropertyName),
+ "metadata");
+ }
+
+ return GetValidators(metadata, context, property.Attributes.OfType<Attribute>());
+ }
+
+ private IEnumerable<ModelValidator> GetValidatorsForType(ModelMetadata metadata, ControllerContext context)
+ {
+ return GetValidators(metadata, context, GetTypeDescriptor(metadata.ModelType).GetAttributes().Cast<Attribute>());
+ }
+ }
+}