FluentValidation:如何将所有验证消息放在一个位置? [英] FluentValidation: How do i put all validation messages at a single location?
问题描述
这是我的验证课程之一:
This is one of my validation class:
public class StocksValidator : AbstractValidator<Stocks>
{
public StocksValidator()
{
RuleFor(x => x.SellerId).GreaterThan(1).WithMessage("SellerId should be greater than 1")
.LessThan(100).WithMessage("SellerId should be less than 100");
RuleFor(x => x.SellerType).GreaterThan(101).WithMessage("SellerType should be greater than 101")
.LessThan(200).WithMessage("SellerType should be less than 200");
RuleFor(x => x.SourceId).GreaterThan(201).WithMessage("SourceId should be greater than 201")
.LessThan(300).WithMessage("SourceId should be less than 300");
}
}
我知道这些消息,例如{field}应该小于{x}应该位于公共位置,而不是在此处.但是我不知道如何集中他们?
I understand that these messages like {field} should be less that {x} should be at a common location and not here. But i don't have a clue how to centralize them?
-
一种方法是使用所有这些常量字符串创建新的c#文件.这很简单.
One way could be to create new c# file with all these constant strings. This is fairly simple.
在Web API中使用流利的验证进行本地化.这有什么好处.在哪里可以找到很好的教程?
Using localization in web api with fluent validation. What are benefits of this. Where do i find its good tutorial?
推荐答案
如果您需要更改内置规则的默认消息,则将影响所有包含此规则的验证器 (s)—遵循以下步骤:
If you need to change default messages for built-in rule(s), that will affect all validators, which contain this rule(s) — follow next steps:
1 :使用位于Startup.cs
或global.asax.cs
ValidatorOptions.ResourceProviderType = typeof(MyResourceProvider);
2 :为某些验证规则覆盖默认消息
2: override default messages for some validation rules
// create MyResourceProvider.resx to auto-generate this class in MyResourceProvider.Designer.cs file (support multiple cultures out of box),
// or create class manually and specify messages in code
public class MyResourceProvider {
public static string greaterthan_error {
get {
return "{PropertyName} should be greater than {ComparisonValue}, but you entered {PropertyValue}";
}
}
public static string lessthan_error {
get {
return "{PropertyName} should be less than {ComparisonValue}";
}
}
}
3(可选):使用WithName()
方法将属性名称的默认输出替换为更加用户友好
3 (optional): use WithName()
method to replace default output of property name with more user-friendly
RuleFor(x => x.SellerId).GreaterThan(1).WithName("Seller identidier")
// outputs "Seller identidier should be greater than 1, but you entered 0"
更多信息,请参见 FluentValidation github :
1.本地化-在这里您可以找到有关消息本地化方式(如WithLocalizedMessage
方法)以及资源名称的更多信息,这些资源名称应在MyResourceProvider
中用作属性名称.
1. Localization — here you can find more info about ways to localize messages (like WithLocalizedMessage
method), and also resource names, that should be used as property names in MyResourceProvider
.
2.内置验证器-在这里您可以找到所有验证规则的替代名称,这些替代规则应在错误消息字符串中使用.
2. Built in Validators - here you can find substitution names for all validation rules, that should be used in error message strings.
3. Messages.resx -默认资源文件,其中包含错误消息.
3. Messages.resx - default resource file with error messages placed here.
这篇关于FluentValidation:如何将所有验证消息放在一个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!