Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Validation using Fluent Validation

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?

like image 478
The Sheek Geek Avatar asked Nov 10 '11 18:11

The Sheek Geek


People also ask

What is fluent validation used for?

What is Fluent Validation? Fluent Validation is a validation library for . NET, used for building strongly typed validation rules for business objects. Fluent validation is one way of setting up dedicated validator objects, that you would use when you want to treat validation logic as separate from business logic.

What is FluentValidation C#?

FluentValidation is a .NET library for building strongly-typed validation rules. It Uses a fluent interface and lambda expressions for building validation rules. It helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.

Is fluent validation client side?

FluentValidation is a server-side library and does not provide any client-side validation directly. However, it can provide metadata which can be applied to the generated HTML elements for use with a client-side framework such as jQuery Validate in the same way that ASP. NET's default validation attributes work.

What is fluent validation in MVC?

Fluent validation is one way of setting up dedicated validator objects that you can use when you want to treat validation logic as separate from the business logic. The Aspect-Oriented Programming (AOP) paradigm enables separation of cross-cutting concerns within a system and validation is one such concern.


1 Answers

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); }); 
like image 123
Denis Pitcher Avatar answered Sep 23 '22 03:09

Denis Pitcher