/// <inheritdoc />
        public void CreateValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            IStringLocalizer stringLocalizer = null;

            if (_options.Value.DataAnnotationLocalizerProvider != null && _stringLocalizerFactory != null)
            {
                // This will pass first non-null type (either containerType or modelType) to delegate.
                // Pass the root model type(container type) if it is non null, else pass the model type.
                stringLocalizer = _options.Value.DataAnnotationLocalizerProvider(
                    context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType,
                    _stringLocalizerFactory);
            }

            var hasRequiredAttribute = false;

            for (var i = 0; i < context.Results.Count; i++)
            {
                var validatorItem = context.Results[i];
                if (validatorItem.Validator != null)
                {
                    // Check if a required attribute is already cached.
                    hasRequiredAttribute |= validatorItem.Validator is RequiredAttributeAdapter;
                    continue;
                }

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

                hasRequiredAttribute |= attribute is RequiredAttribute;

                var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer);
                if (adapter != null)
                {
                    validatorItem.Validator  = adapter;
                    validatorItem.IsReusable = true;
                }
            }

            if (!hasRequiredAttribute && context.ModelMetadata.IsRequired)
            {
                // Add a default '[Required]' validator for generating HTML if necessary.
                context.Results.Add(new ClientValidatorItem
                {
                    Validator = _validationAttributeAdapterProvider.GetAttributeAdapter(
                        new RequiredAttribute(),
                        stringLocalizer),
                    IsReusable = true
                });
            }
        }
        /// <summary>
        /// Creates set of <see cref="IClientModelValidator"/>s by updating
        /// <see cref="ClientValidatorItem.Validator"/> in <see cref="ClientValidatorProviderContext.Results"/>.
        /// </summary>
        /// <param name="context">The <see cref="ClientModelValidationContext"/> associated with this call.</param>
        public void CreateValidators(ClientValidatorProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var type       = context.ModelMetadata.ContainerType ?? context.ModelMetadata.ModelType;
            var isReusable = _configurationContext.ModelMetadataProviders.UseCachedProviders;
            var flag       = false;

            foreach (var result in context.Results)
            {
                if (result.Validator != null)
                {
                    flag |= result.Validator is RequiredAttributeAdapter;
                }
                else
                {
                    if (result.ValidatorMetadata is ValidationAttribute validatorMetadata)
                    {
                        flag |= validatorMetadata is RequiredAttribute;
                        var attributeAdapter = _validationAttributeAdapterProvider.GetAttributeAdapter(validatorMetadata,
                                                                                                       new ValidationStringLocalizer(type,
                                                                                                                                     context.ModelMetadata.PropertyName,
                                                                                                                                     validatorMetadata,
                                                                                                                                     _localizationProvider,
                                                                                                                                     _keyBuilder,
                                                                                                                                     _expressionHelper));

                        if (attributeAdapter != null)
                        {
                            result.Validator  = attributeAdapter;
                            result.IsReusable = isReusable;
                        }
                    }
                }
            }

            if (flag || !context.ModelMetadata.IsRequired)
            {
                return;
            }

            context.Results.Add(new ClientValidatorItem
            {
                Validator = _validationAttributeAdapterProvider.GetAttributeAdapter(
                    new RequiredAttribute(),
                    new ValidationStringLocalizer(type,
                                                  context.ModelMetadata.PropertyName,
                                                  new RequiredAttribute(),
                                                  _localizationProvider,
                                                  _keyBuilder,
                                                  _expressionHelper)),
                IsReusable = isReusable
            });
        }
        public void AdapterFactory_RegistersAdapters_ForDataAnnotationAttributes(
            ValidationAttribute attribute,
            Type expectedAdapterType)
        {
            // Arrange and Act
            var adapter = _validationAttributeAdapterProvider.GetAttributeAdapter(attribute, stringLocalizer: null);

            // Assert
            Assert.IsType(expectedAdapterType, adapter);
        }
示例#4
0
 public IAttributeAdapter GetAttributeAdapter(CPFAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is CPFAttribute)
     {
         return(new CPFAttributeAdapter(attribute as CPFAttribute, stringLocalizer));
     }
     else
     {
         return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
示例#5
0
        private static void AddValidationsForViewModel(EnterYourDetailsBodyViewModel viewModel, ModelExplorer modelExplorer, IValidationAttributeAdapterProvider validationAttributeAdapterProvider, ClientModelValidationContext context)
        {
            var properties = new Dictionary <string, string>()
            {
                { nameof(EnterYourDetailsBodyViewModel.EmailAddress), nameof(EnterYourDetailsBodyViewModel.EmailAddressIsRequired) },
                { nameof(EnterYourDetailsBodyViewModel.TelephoneNumber), nameof(EnterYourDetailsBodyViewModel.TelephoneNumberIsRequired) },
                { nameof(EnterYourDetailsBodyViewModel.CallbackDateOptionSelected), nameof(EnterYourDetailsBodyViewModel.IsCallback) },
                { nameof(EnterYourDetailsBodyViewModel.CallbackTimeOptionSelected), nameof(EnterYourDetailsBodyViewModel.IsCallback) },
            };
            var errorMessages = new Dictionary <string, string>()
            {
                { nameof(EnterYourDetailsBodyViewModel.EmailAddress), "Enter your email address" },
                { nameof(EnterYourDetailsBodyViewModel.TelephoneNumber), "Enter your telephone number" },
                { nameof(EnterYourDetailsBodyViewModel.CallbackDateOptionSelected), EnterYourDetailsBodyViewModel.CallbackDateOptionValidationError },
                { nameof(EnterYourDetailsBodyViewModel.CallbackTimeOptionSelected), EnterYourDetailsBodyViewModel.CallbackTimeOptionValidationError },
            };

            if (properties.Keys.Contains(modelExplorer.Metadata.PropertyName))
            {
                var propertyInfo = viewModel.GetType().GetProperty(properties[modelExplorer.Metadata.PropertyName]);

                if (propertyInfo != null)
                {
                    var isRequiredValue = (bool?)propertyInfo.GetValue(viewModel, null);

                    if (isRequiredValue != null && isRequiredValue.Value)
                    {
                        var validationAdapter = (RequiredAttributeAdapter)validationAttributeAdapterProvider.GetAttributeAdapter(new RequiredAttribute(), null);
                        var errorMessage      = errorMessages[modelExplorer.Metadata.PropertyName];
                        validationAdapter.Attribute.ErrorMessage = errorMessage;
                        validationAdapter.AddValidation(context);
                    }
                }
            }
        }
示例#6
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute,
                                                     IStringLocalizer stringLocalizer)
        {
            if (attribute is RequiredUnlessAttribute ||
                attribute is RequiredWhenAttribute)
            {
                return(new RequiredAttributeAdapter(attribute as RequiredAttribute, stringLocalizer));
            }


            if (attribute is RangeUnlessAttribute ||
                attribute is RangeWhenAttribute)
            {
                return(new RangeAttributeAdapter(attribute as RangeAttribute, stringLocalizer));
            }


            if (attribute is StringLengthUnlessAttribute ||
                attribute is StringLengthWhenAttribute)
            {
                return(new StringLengthAttributeAdapter(attribute as StringLengthAttribute, stringLocalizer));
            }

            else
            {
                return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
            }
        }
示例#7
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            var fpAttr = attribute as ModelAwareValidationAttribute;

            return(fpAttr != null
                ? new FoolproofAttributeAdapter(fpAttr, stringLocalizer)
                : baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
 public IAttributeAdapter GetAttributeAdapter(
     ValidationAttribute attribute,
     IStringLocalizer stringLocalizer)
 {
     return(attribute is PeopleAttribute
         ? new PeopleAttributeAdapter(attribute as PeopleAttribute, stringLocalizer)
         : baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is DateRangeAttribute)
     {
         return(new DateRangeAttributeAdapter(attribute as DateRangeAttribute, stringLocalizer));
     }
     return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is CpfAttribute cpfAttribute)
     {
         return(new CpfAttributeAdapter(cpfAttribute, stringLocalizer));
     }
     return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
示例#11
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is CurrencyAttribute currencyAttribute)
     {
         return(new CurrencyAttributeAdapter((CurrencyAttribute)attribute, stringLocalizer));
     }
     return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is TextMatchAttribute textMatchAttribute)
     {
         return(new TextMatchAttributeAdapter(textMatchAttribute, stringLocalizer));
     }
     return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
示例#13
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is MoedaAttribute moedaAttribute)
     {
         return(new MoedaAttributeAdapter(moedaAttribute, stringLocalizer));
     }
     return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
示例#14
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is StopWordsAttribute)
            {
                return(new StopWordsAttributeAdapter(attribute as StopWordsAttribute, stringLocalizer));
            }

            return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
 public IAttributeAdapter GetAttributeAdapter(
     ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is CustomValidationAttribute attr)
     {
         return(new CustomValidationAttributeProvider(attr, stringLocalizer));
     }
     return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
 }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is DocumentoAttribute documentoAttribute)
            {
                return(new DocumentoAttributeAdapter(documentoAttribute, stringLocalizer));
            }

            return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
示例#17
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is DeviseAtribut DeviseAtribut)
            {
                return(new DeviseAtributAdapter(DeviseAtribut, stringLocalizer));
            }

            return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
示例#18
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is DecimalAttribute decimalAttribute)
            {
                return(new DecimalAttributeAdapter(decimalAttribute, stringLocalizer));
            }

            return(_adapterProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is EmailAttribute EmailAttribute)
            {
                return(new EmailAttributeAdapter(EmailAttribute, stringLocalizer));
            }

            return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
示例#20
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            if (attribute is TestAttribute classicMovieAttribute)
            {
                return(new TestAttributeAdapter(classicMovieAttribute, stringLocalizer));
            }

            return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
示例#21
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute,
                                                     IStringLocalizer stringLocalizer)
        {
            if (attribute is DateIsNotInThePastAttribute dateAttribute)
            {
                return(new DateIsNotBookedAttributeProvider(dateAttribute, stringLocalizer));
            }

            return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
        }
        public void GetAttributeAdapter_returns_adapter_for_attribute(ValidationAttribute attribute, Type expectedAdapterType)
        {
            // Arrange

            // Act
            var adapter = _provider.GetAttributeAdapter(attribute, Substitute.For <IStringLocalizer>());

            // Assert
            adapter.ShouldBeOfType(expectedAdapterType);
        }
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is RequiredAttribute)
     {
         return(new RequiredAttributeAdapter((RequiredAttribute)attribute, stringLocalizer));
     }
     else
     {
         return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            switch (attribute)
            {
            case PastAttribute past:
                return(new PastAttributeAdapter(past, stringLocalizer));

            default:
                return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
            }
        }
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
        {
            switch (attribute)
            {
            case RequiredIfAttribute _:
                return(new RequiredIfAttributeAdapter(attribute as RequiredIfAttribute, stringLocalizer));

            default:
                return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
            }
        }
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is UserProfileAttribute)
     {
         return(new UserProfileAttributeAdapter(attribute as UserProfileAttribute, stringLocalizer));
     }
     else
     {
         return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
示例#27
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is PasswordValidator)
     {
         return(new PasswordValidatorAttributeAdapter(attribute as PasswordValidator, stringLocalizer));
     }
     else
     {
         return(_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
示例#28
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is PeopleAttribute)
     {
         return(new ClassicMovieAttributeAdapter(attribute as PeopleAttribute, stringLocalizer));
     }
     else
     {
         return(baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
示例#29
0
 public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute, IStringLocalizer stringLocalizer)
 {
     if (attribute is CustomUriAttribute uriAttribute)
     {
         return(new CustomUriAttributeAdapter(uriAttribute, stringLocalizer));
     }
     else
     {
         return(m_baseProvider.GetAttributeAdapter(attribute, stringLocalizer));
     }
 }
示例#30
0
        public IAttributeAdapter GetAttributeAdapter(ValidationAttribute attribute,
                                                     IStringLocalizer stringLocalizer)
        {
            var retVal = options.Value.TryGetAdapter(attribute, stringLocalizer);

            if (retVal == null)
            {
                retVal = baseProvider.GetAttributeAdapter(attribute, stringLocalizer);
            }

            return(retVal);
        }