public void CreateValidators(ClientValidatorProviderContext context)
        {
            for (var i = 0; i < context.Results.Count; i++)
            {
                var validatorItem = context.Results[i];
                if (validatorItem.Validator != null)
                {
                    continue;
                }

                var attribute = validatorItem.ValidatorMetadata as ValidationAttribute;
                if (attribute == null)
                {
                    continue;
                }

                var validationAdapterProvider = new ValidationAttributeAdapterProvider();

                validatorItem.Validator = validationAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

                if (attribute is RequiredAttribute)
                {
                    validatorItem.IsReusable = true;
                }
            }
        }
Esempio n. 2
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     return(attribute switch
     {
         RequiredOrAttribute requiredAttribute => new RequiredOrAdatpter(requiredAttribute, stringLocalizer),
         MaxFileSizeAttribute maxFileSizeAttribute => new MaxFileSizeAttributeAdapter(maxFileSizeAttribute, stringLocalizer),
         AllowTypesAttribute allowTypesAttribute => new AllowTypesAttributeAdapter(allowTypesAttribute, stringLocalizer),
         _ => defaultProvider.GetAttributeAdapter(attribute, stringLocalizer)
     });
Esempio n. 3
0
        private static IDictionary <string, object> GetUnobtrusiveValidationAttributes(PropertyInfo propertyInfo, ModelMetadata modelMetadata, IModelMetadataProvider provider)
        {
            var hasRequiredAttribute = false;
            var propType             = GetPropertyType(propertyInfo);
            var attrs = new AttributeDictionary();

            var valAttrs = propertyInfo.GetCustomAttributes(typeof(ValidationAttribute));

            ValidationAttributeAdapterProvider adapterProvider = new ValidationAttributeAdapterProvider();
            IAttributeAdapter adapter;

            var context = new ClientModelValidationContext(
                new ActionContext(),
                modelMetadata,
                provider,
                attrs);

            foreach (ValidationAttribute valAttr in valAttrs)
            {
                if (valAttr is RequiredAttribute)
                {
                    hasRequiredAttribute = true;
                }

                adapter = adapterProvider.GetAttributeAdapter(valAttr, null);
                adapter.AddValidation(context);
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                adapter = adapterProvider.GetAttributeAdapter(new RequiredAttribute(), null);
                adapter.AddValidation(context);
            }

            if (propType == typeof(float) ||
                propType == typeof(double) ||
                propType == typeof(decimal))
            {
                MergeAttribute(context.Attributes, "data-val", "true");
                MergeAttribute(context.Attributes, "data-val-number", GetErrorMessage(context.ModelMetadata));
            }

            return(attrs.ToDictionary(p => p.Key, pp => (object)pp.Value));
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            attribute.ErrorMessage = "Validation_" + attribute.GetType().Name.Replace("Attribute", string.Empty);

            // You might need this if you have custom DataTypeAttribute, for us this just creates
            // "EmailAddress_EmailAddress" instead of "EmailAddress" as key.
            //if (attribute is DataTypeAttribute dataTypeAttribute)
            //    attribute.ErrorMessage += "_" + dataTypeAttribute.DataType;

            return(_originalProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
Esempio n. 5
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            IValidationAttributeAdapterProvider _baseProvider = new ValidationAttributeAdapterProvider();

            if (attribute is MoedaAttribute moedaAttribute)
            {
                return(new MoedaAttributeAdapter(moedaAttribute, stringLocalizer));
            }

            return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
        /// <summary>
        /// Creates an <see cref="IAttributeAdapter" /> for the given attribute.
        /// </summary>
        /// <param name="attribute">The attribute to create an adapter for.</param>
        /// <param name="stringLocalizer">The localizer to provide to the adapter.</param>
        /// <returns>An <see cref="IAttributeAdapter" /> for the given attribute.</returns>
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            Requires.NotNull(attribute, nameof(attribute));

            AdapterFactory factory;

            if (factories.TryGetValue(attribute.GetType(), out factory))
            {
                return(factory(attribute, stringLocalizer));
            }

            var inner = new ValidationAttributeAdapterProvider();

            return(inner.GetAttributeAdapter(attribute, stringLocalizer));
        }
Esempio n. 7
0
        public virtual IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            var type = attribute.GetType();

            if (type == typeof(DynamicStringLengthAttribute))
            {
                return(new DynamicStringLengthAttributeAdapter((DynamicStringLengthAttribute)attribute, stringLocalizer));
            }

            if (type == typeof(DynamicMaxLengthAttribute))
            {
                return(new DynamicMaxLengthAttributeAdapter((DynamicMaxLengthAttribute)attribute, stringLocalizer));
            }

            return(_defaultAdapter.GetAttributeAdapter(attribute, stringLocalizer));
        }
Esempio n. 8
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (!(attribute is DataTypeAttribute))
            {
                attribute.ErrorMessage = attribute.FormatErrorMessage("{0}");

                if (attribute is MaxLengthAttribute)
                {
                    attribute.ErrorMessage = attribute.ErrorMessage.Replace((attribute as MaxLengthAttribute).Length.ToString(), "{1}");
                }
                else if (attribute is MinLengthAttribute)
                {
                    attribute.ErrorMessage = attribute.ErrorMessage.Replace((attribute as MinLengthAttribute).Length.ToString(), "{1}");
                }
                else if (attribute is StringLengthAttribute)
                {
                    attribute.ErrorMessage = attribute.ErrorMessage.Replace((attribute as StringLengthAttribute).MaximumLength.ToString(), "{1}").Replace((attribute as StringLengthAttribute).MinimumLength.ToString(), "{2}");
                }
            }

            return(originalProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            IAttributeAdapter adapter;

            var type = attribute.GetType();

            if (attribute is RegularExpressionAttribute)
            {
                adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MaxLengthAttribute)
            {
                adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RequiredAttribute)
            {
                adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CompareAttribute)
            {
                adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MinLengthAttribute)
            {
                adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CreditCardAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            }
            else if (attribute is StringLengthAttribute)
            {
                adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RangeAttribute)
            {
                adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
            }
            else if (attribute is EmailAddressAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            }
            else if (attribute is PhoneAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            }
            else if (attribute is UrlAttribute)
            {
                adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            }
            else if (attribute is FileExtensionsAttribute)
            {
                adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer);
            }
            else
            {
                adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }

            return(adapter);
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException(nameof(attribute));
            }

            IAttributeAdapter adapter = null;

            var type = attribute.GetType();

            //.NET Core 2.2
            //if (attribute is RegularExpressionAttribute)
            //{
            //    adapter = new RegularExpressionAttributeAdapter((RegularExpressionAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is MaxLengthAttribute)
            //{
            //    adapter = new MaxLengthAttributeAdapter((MaxLengthAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is RequiredAttribute)
            //{
            //    adapter = new Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is CompareAttribute)
            //{
            //    adapter = new CompareAttributeAdapter((CompareAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is MinLengthAttribute)
            //{
            //    adapter = new MinLengthAttributeAdapter((MinLengthAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is CreditCardAttribute)
            //{
            //    adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            //}
            //else if (attribute is StringLengthAttribute)
            //{
            //    adapter = new StringLengthAttributeAdapter((StringLengthAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is RangeAttribute)
            //{
            //    adapter = new RangeAttributeAdapter((RangeAttribute)attribute, stringLocalizer);
            //}
            //else if (attribute is EmailAddressAttribute)
            //{
            //    adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            //}
            //else if (attribute is PhoneAttribute)
            //{
            //    adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            //}
            //else if (attribute is UrlAttribute)
            //{
            //    adapter = new DataTypeAttributeAdapter((DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            //}
            //else if (attribute is FileExtensionsAttribute)
            //{
            //    adapter = new FileExtensionsAttributeAdapter((FileExtensionsAttribute)attribute, stringLocalizer);
            //}
            //else
            //{
            //    adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            //}

            if (attribute is RegularExpressionAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RegularExpressionAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RegularExpressionAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MaxLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MaxLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MaxLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RequiredAttribute)
            {
                adapter = new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CompareAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.CompareAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (CompareAttribute)attribute, stringLocalizer);
            }
            else if (attribute is MinLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.MinLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (MinLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is CreditCardAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-creditcard", stringLocalizer);
            }
            else if (attribute is StringLengthAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.StringLengthAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (StringLengthAttribute)attribute, stringLocalizer);
            }
            else if (attribute is RangeAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.RangeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (RangeAttribute)attribute, stringLocalizer);
            }
            else if (attribute is EmailAddressAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-email", stringLocalizer);
            }
            else if (attribute is PhoneAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-phone", stringLocalizer);
            }
            else if (attribute is UrlAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.DataTypeAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (DataTypeAttribute)attribute, "data-val-url", stringLocalizer);
            }
            else if (attribute is FileExtensionsAttribute)
            {
                adapter = (IAttributeAdapter)Activator.CreateInstance(Type.GetType("Microsoft.AspNetCore.Mvc.DataAnnotations.FileExtensionsAttributeAdapter, Microsoft.AspNetCore.Mvc.DataAnnotations"), (FileExtensionsAttribute)attribute, stringLocalizer);
            }
            else
            {
                adapter = defaultClientModelValidatorProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }

            return(adapter);
        }
Esempio n. 11
0
            public void CreateValidators(ClientValidatorProviderContext context)
            {
                for (var i = 0; i < context.Results.Count; i++)
                {
                    var validatorItem = context.Results[i];
                    if (validatorItem.Validator != null)
                    {
                        continue;
                    }

                    var attribute = validatorItem.ValidatorMetadata as ValidationAttribute;
                    if (attribute == null)
                    {
                        continue;
                    }

                    var validationAdapterProvider = new ValidationAttributeAdapterProvider();

                    validatorItem.Validator = validationAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

                    if (attribute is RequiredAttribute)
                    {
                        validatorItem.IsReusable = true;
                    }
                }
            }
Esempio n. 12
0
            public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
            {
                attribute.ErrorMessage = attribute.FormatErrorMessage("{0}");

                return(_originalProvider.GetAttributeAdapter(attribute, stringLocalizer));
            }