使用流利的驗證并驗證三個變數中的至少一個是否> 0。我嘗試使用when otherwise陳述句進行驗證,但無法添加另一個otherwise陳述句。
When(c => c.var1!= null || c.var2!= null || c.var3!= null, () =>
{
RuleFor(c => c.var1).GreaterThan(0)
.WithMessage("One of these fields, var1, var2, or var3, are required.");
}).Otherwise(() =>
{
RuleFor(c => c.var2).GreaterThan(0);
});
// I would like to verify a third var but can't stack otherwise statements
如果我想驗證 var1、var2、var3 之一是否 > 0,我應該進行哪些更改?
uj5u.com熱心網友回復:
如果您使用 when 陳述句,則只需發送一條驗證訊息即可驗證所有 3 個變數。有諸如依賴規則之類的東西,但這更復雜。
RuleFor(c => c.var1).NotNull()
.WithMessage("At least one is required");
RuleFor(c => c.var2).NotNull()
.When(c => c.var2!= null)
.WithMessage("At least one is required");
RuleFor(c => c.var3).NotNull()
.When(c => c.var1!= null && c.var2!= null)
.WithMessage("At least one is required");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/495291.html
