1

I'm building custom validation response from my API with localization. I need to localize default error messages for variables on model binding when requesting API. I localized almost everything, but I still get The X field is required in English.

I used solution from this response and it works well with .NET 7, however this options configurator misses message for The X field is required.

I also found solution with adding [Required] attribute and overriding its message, but application is in advanced state, so this would take a lot of time.

ervin
  • 329
  • 1
  • 2
  • 12

2 Answers2

1

When using FluentValidation you can define the rules as follows: RuleFor(v => v.Item) .NotNull() .WithMessage("'Item' cannot be null.");

Provide the localization inside the WithMessage().

  • 1
    similar to `[Required]`, it would make me refactor whole solution, this is what I try to avoid – ervin May 29 '23 at 21:43
0

why not using fluentvalidation docs.fluentvalidation.net/en/latest/aspnet.html

user123456
  • 2,524
  • 7
  • 30
  • 57
  • I am using FluentValidation, but you still need to provide translations for base errors – ervin May 28 '23 at 22:22