0

I'm trying to override the default message of the SetAttemptedValueIsInvalidAccessor(Func<String,String,String>) in the startup.cs

The default message,

services.AddMvcCore().AddMvcOptions(opt => 
{
opt.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor(x,y) => $"The value {y} is not valid for {x}."
}

Is there a way I could override this message to include range ?

services.AddMvcCore().AddMvcOptions(opt => 
{
opt.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor(x,y) => $"The value for {y} must be between {range1} and {range2}."
}

I tried writing a custom attribute. However that won't be triggered unless if there a way to suppress only this model validation.

Dev
  • 1,451
  • 20
  • 30
  • According to your description, I suppose you want to localize Range attribute, if that is the case, you could try to use ErrorMessageResourceType and ErrorMessageResourceName to use a resource for a Data Annotation. Like this `[Range(1, 150, ErrorMessageResourceType = typeof(MyApp.Properties.Resource), ErrorMessageResourceName = "ErrorMessageMustBeBetween")]`. Reference: [How to localize Range attribute](https://stackoverflow.com/questions/25160778/) and [Localizing DataAnnotations](http://www.ziyad.info/en/articles/16-Localizing_DataAnnotations) – Zhi Lv Sep 21 '20 at 15:58
  • @Zhi Lv I tried using the range attribute and even overriding the isvalid method. It doesn't trigger range attribute validation as it does other validation on fields before it.( I'm testing by inputting the values greater than int.MaxValue, alphabets, special characters etc in postman for the field of int type ). – Dev Sep 21 '20 at 22:45

0 Answers0