FluentValidation:使用其他属性的值自定义错误消息
FluentValidation: Customizing the error message with values from other properties
我有这个:
RuleForEach(inputData => inputData.Loads).ChildRules(inputData => {
inputData.RuleFor(load => load.Asnow).GreaterThanOrEqualTo(0).WithMessage("no negative snow allowed"));
.... etc
现在我想在消息中表达,消息是关于负载集合中的哪些负载。
"load" 有一个 属性 "LoadName",我想在消息中包含它的值,例如
$"{load.LoadName} no negative snow allowed"
我该怎么做?
使用 WithMessage(Func<T, string>)
重载:
RuleForEach(inputData => inputData.Loads)
.ChildRules(inputData =>
{
inputData.RuleFor(load => load.Asnow).GreaterThanOrEqualTo(0).WithMessage(load => $"{load.LoadName} no negative snow allowed");
});
我有这个:
RuleForEach(inputData => inputData.Loads).ChildRules(inputData => {
inputData.RuleFor(load => load.Asnow).GreaterThanOrEqualTo(0).WithMessage("no negative snow allowed"));
.... etc
现在我想在消息中表达,消息是关于负载集合中的哪些负载。
"load" 有一个 属性 "LoadName",我想在消息中包含它的值,例如
$"{load.LoadName} no negative snow allowed"
我该怎么做?
使用 WithMessage(Func<T, string>)
重载:
RuleForEach(inputData => inputData.Loads)
.ChildRules(inputData =>
{
inputData.RuleFor(load => load.Asnow).GreaterThanOrEqualTo(0).WithMessage(load => $"{load.LoadName} no negative snow allowed");
});