英文:
How to localize 'The X field is required' native error in .NET API?
问题
我正在从我的API构建自定义验证响应,并进行本地化。我需要本地化在请求API时绑定模型变量的默认错误消息。我已经本地化了几乎所有内容,但仍然会收到The X field is required
的英文错误消息。
我使用了来自此回答的解决方案,它在.NET 7中运行良好,但此选项配置器缺少The X field is required
的消息。
我还找到了使用[Required]
属性并覆盖其消息的解决方案,但应用程序处于高级状态,因此这将需要很多时间。
英文:
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.
答案1
得分: 1
在使用FluentValidation时,您可以定义规则如下:
RuleFor(v => v.Item) .NotNull() .WithMessage("'Item' 不能为空。");
英文:
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().
答案2
得分: 0
"Why not using FluentValidation docs.fluentvalidation.net/en/latest/aspnet.html"
英文:
why not using fluentvalidation docs.fluentvalidation.net/en/latest/aspnet.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论