/// <summary> /// Initializes a new <see cref="DefaultValidationHtmlAttributeProvider"/> instance. /// </summary> /// <param name="optionsAccessor">The accessor for <see cref="MvcViewOptions"/>.</param> /// <param name="metadataProvider">The <see cref="IModelMetadataProvider"/>.</param> /// <param name="clientValidatorCache">The <see cref="ClientValidatorCache"/> that provides /// a list of <see cref="IClientModelValidator"/>s.</param> public DefaultValidationHtmlAttributeProvider( IOptions <MvcViewOptions> optionsAccessor, IModelMetadataProvider metadataProvider, ClientValidatorCache clientValidatorCache) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); } if (clientValidatorCache == null) { throw new ArgumentNullException(nameof(clientValidatorCache)); } _clientValidatorCache = clientValidatorCache; _metadataProvider = metadataProvider; var clientValidatorProviders = optionsAccessor.Value.ClientModelValidatorProviders; _clientModelValidatorProvider = new CompositeClientModelValidatorProvider(clientValidatorProviders); }
/// <summary> /// Initializes a new <see cref="DefaultValidationHtmlAttributeProvider"/> instance. /// </summary> /// <param name="optionsAccessor">The accessor for <see cref="MvcViewOptions"/>.</param> /// <param name="metadataProvider">The <see cref="IModelMetadataProvider"/>.</param> /// <param name="clientValidatorCache">The <see cref="ClientValidatorCache"/> that provides /// a list of <see cref="IClientModelValidator"/>s.</param> public DefaultValidationHtmlAttributeProvider( IOptions <MvcViewOptions> optionsAccessor, IModelMetadataProvider metadataProvider, #pragma warning disable PUB0001 // Pubternal type in public API ClientValidatorCache clientValidatorCache #pragma warning restore PUB0001 ) { if (optionsAccessor == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } if (metadataProvider == null) { throw new ArgumentNullException(nameof(metadataProvider)); } if (clientValidatorCache == null) { throw new ArgumentNullException(nameof(clientValidatorCache)); } _clientValidatorCache = clientValidatorCache; _metadataProvider = metadataProvider; var clientValidatorProviders = optionsAccessor.Value.ClientModelValidatorProviders; _clientModelValidatorProvider = new CompositeClientModelValidatorProvider(clientValidatorProviders); }
public CustomValidationHtmlAttributeProvider(IOptions <MvcViewOptions> optionsAccessor, IModelMetadataProvider metadataProvider, ClientValidatorCache clientValidatorCache) : base(optionsAccessor, metadataProvider, clientValidatorCache) { _metadataProvider = metadataProvider; _clientValidatorCache = clientValidatorCache; var clientValidatorProviders = optionsAccessor.Value.ClientModelValidatorProviders; _clientModelValidatorProvider = new CompositeClientModelValidatorProvider(clientValidatorProviders); }
// Creates a provider with all the defaults - includes data annotations public static IClientModelValidatorProvider CreateDefaultProvider() { var providers = new IClientModelValidatorProvider[] { new DefaultClientModelValidatorProvider(), new DataAnnotationsClientModelValidatorProvider(), }; return(new TestClientModelValidatorProvider(providers)); }
// Creates a provider with all the defaults - includes data annotations public static IClientModelValidatorProvider CreateDefaultProvider() { var providers = new IClientModelValidatorProvider[] { new DefaultClientModelValidatorProvider(), new DataAnnotationsClientModelValidatorProvider(), }; return new TestClientModelValidatorProvider(providers); }
// Creates a provider with all the defaults - includes data annotations public static IClientModelValidatorProvider CreateDefaultProvider() { var providers = new IClientModelValidatorProvider[] { new DefaultClientModelValidatorProvider(), new DataAnnotationsClientModelValidatorProvider( new TestOptionsManager<MvcDataAnnotationsLocalizationOptions>(), stringLocalizerFactory: null), }; return new TestClientModelValidatorProvider(providers); }
// Creates a provider with all the defaults - includes data annotations public static IClientModelValidatorProvider CreateDefaultProvider() { var providers = new IClientModelValidatorProvider[] { new DefaultClientModelValidatorProvider(), new DataAnnotationsClientModelValidatorProvider( new TestOptionsManager <MvcDataAnnotationsLocalizationOptions>(), stringLocalizerFactory: null), }; return(new TestClientModelValidatorProvider(providers)); }
// Creates a provider with all the defaults - includes data annotations public static IClientModelValidatorProvider CreateDefaultProvider() { var providers = new IClientModelValidatorProvider[] { new DefaultClientModelValidatorProvider(), new DataAnnotationsClientModelValidatorProvider( new ValidationAttributeAdapterProvider(), Options.Create(new MvcDataAnnotationsLocalizationOptions()), stringLocalizerFactory: null), }; return(new TestClientModelValidatorProvider(providers)); }
public IReadOnlyList<IClientModelValidator> GetValidators(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider) { CacheEntry entry; if (_cacheEntries.TryGetValue(metadata, out entry)) { return GetValidatorsFromEntry(entry, metadata, validatorProvider); } var items = new List<ClientValidatorItem>(metadata.ValidatorMetadata.Count); for (var i = 0; i < metadata.ValidatorMetadata.Count; i++) { items.Add(new ClientValidatorItem(metadata.ValidatorMetadata[i])); } ExecuteProvider(validatorProvider, metadata, items); var validators = ExtractValidators(items); var allValidatorsCached = true; for (var i = 0; i < items.Count; i++) { var item = items[i]; if (!item.IsReusable) { item.Validator = null; allValidatorsCached = false; } } if (allValidatorsCached) { entry = new CacheEntry(validators); } else { entry = new CacheEntry(items); } _cacheEntries.TryAdd(metadata, entry); return validators; }
private void ExecuteProvider(IClientModelValidatorProvider validatorProvider, ModelMetadata metadata, List <ClientValidatorItem> items) { var context = new ClientValidatorProviderContext(metadata, items); validatorProvider.CreateValidators(context); }
private IReadOnlyList <IClientModelValidator> GetValidatorsFromEntry(CacheEntry entry, ModelMetadata metadata, IClientModelValidatorProvider validationProvider) { Debug.Assert(entry.Validators != null || entry.Items != null); if (entry.Validators != null) { return(entry.Validators); } var items = new List <ClientValidatorItem>(entry.Items.Count); for (var i = 0; i < entry.Items.Count; i++) { var item = entry.Items[i]; if (item.IsReusable) { items.Add(item); } else { items.Add(new ClientValidatorItem(item.ValidatorMetadata)); } } ExecuteProvider(validationProvider, metadata, items); return(ExtractValidators(items)); }
public IReadOnlyList <IClientModelValidator> GetValidators(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider) { if (_cacheEntries.TryGetValue(metadata, out var entry)) { return(GetValidatorsFromEntry(entry, metadata, validatorProvider)); } var items = new List <ClientValidatorItem>(metadata.ValidatorMetadata.Count); for (var i = 0; i < metadata.ValidatorMetadata.Count; i++) { items.Add(new ClientValidatorItem(metadata.ValidatorMetadata[i])); } ExecuteProvider(validatorProvider, metadata, items); var validators = ExtractValidators(items); var allValidatorsCached = true; for (var i = 0; i < items.Count; i++) { var item = items[i]; if (!item.IsReusable) { item.Validator = null; allValidatorsCached = false; } } if (allValidatorsCached) { entry = new CacheEntry(validators); } else { entry = new CacheEntry(items); } _cacheEntries.TryAdd(metadata, entry); return(validators); }
/// <summary> /// Gets the <see cref="IClientModelValidator"/> for the metadata from the cache, using the validatorProvider to create when needed. /// </summary> /// <param name="metadata">The <see cref="ModelMetadata"/> being validated.</param> /// <param name="validatorProvider">The <see cref="IClientModelValidatorProvider"/> which will be used to create validators when needed.</param> /// <returns>The list of <see cref="IClientModelValidator"/>s.</returns> public IReadOnlyList <IClientModelValidator> GetValidators(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider) { if (metadata.MetadataKind == ModelMetadataKind.Property && metadata.ContainerMetadata?.BoundConstructor != null && metadata.ContainerMetadata.BoundConstructorPropertyMapping.TryGetValue(metadata, out var parameter)) { // "metadata" typically points to properties. When working with record types, we want to read validation details from the // constructor parameter instead. So let's switch it out. metadata = parameter; } if (_cacheEntries.TryGetValue(metadata, out var entry)) { return(GetValidatorsFromEntry(entry, metadata, validatorProvider)); } var items = new List <ClientValidatorItem>(metadata.ValidatorMetadata.Count); for (var i = 0; i < metadata.ValidatorMetadata.Count; i++) { items.Add(new ClientValidatorItem(metadata.ValidatorMetadata[i])); } ExecuteProvider(validatorProvider, metadata, items); var validators = ExtractValidators(items); var allValidatorsCached = true; for (var i = 0; i < items.Count; i++) { var item = items[i]; if (!item.IsReusable) { item.Validator = null; allValidatorsCached = false; } } if (allValidatorsCached) { entry = new CacheEntry(validators); } else { entry = new CacheEntry(items); } _cacheEntries.TryAdd(metadata, entry); return(validators); }
private void ExecuteProvider(IClientModelValidatorProvider validatorProvider, ModelMetadata metadata, List<ClientValidatorItem> items) { var context = new ClientValidatorProviderContext(metadata, items); validatorProvider.CreateValidators(context); }
private IReadOnlyList<IClientModelValidator> GetValidatorsFromEntry(CacheEntry entry, ModelMetadata metadata, IClientModelValidatorProvider validationProvider) { Debug.Assert(entry.Validators != null || entry.Items != null); if (entry.Validators != null) { return entry.Validators; } var items = new List<ClientValidatorItem>(entry.Items.Count); for (var i = 0; i < entry.Items.Count; i++) { var item = entry.Items[i]; if (item.IsReusable) { items.Add(item); } else { items.Add(new ClientValidatorItem(item.ValidatorMetadata)); } } ExecuteProvider(validationProvider, metadata, items); return ExtractValidators(items); }
public IReadOnlyList <IClientModelValidator> GetValidatorsWithoutCache(ModelMetadata metadata, IClientModelValidatorProvider validatorProvider) { var items = new List <ClientValidatorItem>(metadata.ValidatorMetadata.Count); for (var i = 0; i < metadata.ValidatorMetadata.Count; i++) { items.Add(new ClientValidatorItem(metadata.ValidatorMetadata[i])); } ExecuteProvider(validatorProvider, metadata, items); var validators = ExtractValidators(items); foreach (var item in items) { if (!item.IsReusable) { item.Validator = null; } } return(validators); }