使用 Fluent Validation 进行条件验证

发布于 2024-12-15 03:06:15 字数 208 浏览 0 评论 0原文

我需要的是一种根据其他字段是否填写来有条件地验证字段的方法

。我有一个相关的下拉菜单和日期字段。如果未设置任何字段,则表单应通过验证。但是,如果两个字段之一已设置,但另一个未设置,则应触发验证,要求设置另一个字段。

我已经编写了自定义验证类,但它似乎是在单个字段上验证的。有没有办法使用内置验证器来设置我需要的验证?如果没有,是否有使用自定义验证器连接两个字段的好方法?

What I need is a way to conditionally validate fields depending on if other fields are filled in.

Ex. I have a dropdown and a date field that are related. If none of the fields are set then the form should pass validation. However, if one of the two fields are set but the other isn't then the validation should fire, requiring the other field to be set.

I have written custom validation classes but it seems that it is validates on single fields. Is there a way to set up the validation that I need using the built in validators? If not, Is there a good way to connect two fields using a custom validator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-12-22 03:06:15

Fluent 验证支持条件验证,只需使用 When 子句检查辅助字段的值即可:

https ://docs.fluidation.net/en/latest/conditions.html

使用 When/Unless 指定条件 When 和 Until 方法可用于指定控制规则何时生效的条件
应该执行。例如,CustomerDiscount 上的此规则
属性仅在 IsPreferredCustomer 为 true 时执行:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

Unless 方法与 When 完全相反。

您还可以使用 .SetValidator 操作来定义对 NotEmpty 条件进行操作的自定义验证器。

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

如果您需要为多个规则指定相同的条件,那么您
可以调用顶级 When 方法,而不是链接 When 调用
规则末尾:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

这一次,条件将应用于这两个规则。您还可以
链接调用 else ,这将调用与
条件:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});

Fluent validation supports conditional validation, just use the When clause to check the value of the secondary field:

https://docs.fluentvalidation.net/en/latest/conditions.html

Specifying a condition with When/Unless The When and Unless methods can be used to specify conditions that control when the rule
should execute. For example, this rule on the CustomerDiscount
property will only execute when IsPreferredCustomer is true:

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .When(customer => customer.IsPreferredCustomer);

The Unless method is simply the opposite of When.

You may also be able to use the .SetValidator operation to define a custom validator that operates on the NotEmpty condition.

RuleFor(customer => customer.CustomerDiscount)
    .GreaterThan(0)
    .SetValidator(New MyCustomerDiscountValidator);

If you need to specify the same condition for multiple rules then you
can call the top-level When method instead of chaining the When call
at the end of the rule:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
});

This time, the condition will be applied to both rules. You can also
chain a call to Otherwise which will invoke rules that don’t match the
condition:

When(customer => customer.IsPreferred, () => {
   RuleFor(customer => customer.CustomerDiscount).GreaterThan(0);
   RuleFor(customer => customer.CreditCardNumber).NotNull();
}).Otherwise(() => {
  RuleFor(customer => customer.CustomerDiscount).Equal(0);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文