Conditional Validation using Fluent Validation

筅森魡賤 提交于 2019-11-30 02:35:19
Denis Pitcher

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

https://fluentvalidation.net/start#conditions

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!