2

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:

ClientValidatorCache

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 ;).

Cyril Iselin
  • 596
  • 4
  • 20
  • It is not intended that values are changed in the validation. The mechanism does not support this either. If you have dependent validations, use ex. FluentValidation to process complex rules. – Benjamin Abt Jan 30 '20 at 17:26

0 Answers0