Goal: I want to chanage validation definitions at runtime. For example: mark a property as required.
ModelMetaDataProvider:
You have to bypass the caching mechanism from DefaultModelMetaProvier as descriped here: Getting IMetadataDetailsProviders to Run More than Once in ASP.NET Core
=> Perfect, serverside validation works! (You can redefine the ValidationMetadata.IsRequired flag)
But wait, what's about the client-validation (rendered attributes from unobrustive validation like data-required ...)? It get also cached by the ClientValidatorCache implementation. (This class doesn't have virutal methods or a defined interface)
So I created a class inherited from DefaultValidationHtmlAttributeProvider and override the method AddValidationAttributes. The new implementation call my CustomClientValidatorCache with an implementation which doesn't respect the cache.
That's very ugly and there must be a much better way to accomplish that.
So the final questions is:
-> How I can register my own implementation for ClientValidatorCache
or is there a better way to achieve my goal?
(I created a Test-Repo where you can see my current bad 'solution'): https://github.com/orphisAG/DynamicValidationTest/tree/master/DynamicValidationTest
References:
DefaultValidationHtmlAttributeProvider
(edit: Add-References)
Another Try:
Create a ClientModelValidatorProvider (IClientModelValidatorProvider) and set IsReusable to false.
Result:
It work's only if you change ValidationMetaData from initial existing Validator. Because ClientValidatorCache will return the cached initial result ;).