Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:45:36+00:00 2026-05-26T18:45:36+00:00

What I need is a way to conditionally validate fields depending on if other

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:45:37+00:00Added an answer on May 26, 2026 at 6:45 pm

    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);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to update the month value on a dateTime field in
I need a way to bind POJO objects to an external entity, that could
I'm wondering if anybody knows of a way to conditionally execute a program depending
We have a couple of DLLs that need to be put in the GAC
I have built a WCF load balancer/router to conditionally route any one way service
Need a way to allow sorting except for last item with in a list.
I need a way to easily export and then import data in a MySQL
I need a way to recursively delete a folder and its children. Is there
I need a way to build C++ code from UML diagrams and vice versa.
I need a way to represent a 2-D array (a dense matrix) of doubles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.