// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; namespace Microsoft.AspNet.Mvc.ModelBinding.Validation { /// /// An implementation of which provides client validators /// for attributes which derive from . It also provides /// a validator for types which implement . /// The logic to support /// is implemented in . /// public class DataAnnotationsClientModelValidatorProvider : IClientModelValidatorProvider { // A factory for validators based on ValidationAttribute. internal delegate IClientModelValidator DataAnnotationsClientModelValidationFactory(ValidationAttribute attribute); private readonly Dictionary _attributeFactories = BuildAttributeFactoriesDictionary(); internal Dictionary AttributeFactories { get { return _attributeFactories; } } /// public void GetValidators(ClientValidatorProviderContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var hasRequiredAttribute = false; foreach (var attribute in context.ValidatorMetadata.OfType()) { hasRequiredAttribute |= attribute is RequiredAttribute; DataAnnotationsClientModelValidationFactory factory; if (_attributeFactories.TryGetValue(attribute.GetType(), out factory)) { context.Validators.Add(factory(attribute)); } } if (!hasRequiredAttribute && context.ModelMetadata.IsRequired) { // Add a default '[Required]' validator for generating HTML if necessary. context.Validators.Add(new RequiredAttributeAdapter(new RequiredAttribute())); } } private static Dictionary BuildAttributeFactoriesDictionary() { return new Dictionary() { { typeof(RegularExpressionAttribute), (attribute) => new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute) }, { typeof(MaxLengthAttribute), (attribute) => new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute) }, { typeof(MinLengthAttribute), (attribute) => new MinLengthAttributeAdapter((MinLengthAttribute)attribute) }, { typeof(CompareAttribute), (attribute) => new CompareAttributeAdapter((CompareAttribute)attribute) }, { typeof(RequiredAttribute), (attribute) => new RequiredAttributeAdapter((RequiredAttribute)attribute) }, { typeof(RangeAttribute), (attribute) => new RangeAttributeAdapter((RangeAttribute)attribute) }, { typeof(StringLengthAttribute), (attribute) => new StringLengthAttributeAdapter((StringLengthAttribute)attribute) }, { typeof(CreditCardAttribute), (attribute) => new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "creditcard") }, { typeof(EmailAddressAttribute), (attribute) => new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "email") }, { typeof(PhoneAttribute), (attribute) => new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "phone") }, { typeof(UrlAttribute), (attribute) => new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "url") } }; } } }