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 9020815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:06:41+00:00 2026-06-16T05:06:41+00:00

I have this case when I need to validate a model with two validators:

  • 0

I have this case when I need to validate a model with two validators:

1) a BaseValidator that has some common rules.

2) [Variable]CustomValidator which is determined based on one of the Model’s properties.

Code that will show you what I approximately intend to do (of course it does not work since there is not such method as AlsoValidateWith()) is below:

[Validator(typeof(AnimalValidator))]
public class AnimalModel
{
    public string Type { get; set }
    public int NumberOfLegs { get; set; }
    public string Color { get; set; }
    public int NumberOfEyes { get; set; }
    public bool HasWings { get; set; }
}

public class AnimalValidator: AbstractValidator<AnimalModel>
{
    public AnimalValidator()
    {
        RuleFor(x => x.NumberOfEyes).Equal(2);
        RuleFor(x => x).AlsoValidateWith(new DogValidator()).When(x => x.Type == "Dog");
        RuleFor(x => x).AlsoValidateWith(new CatValidator()).When(x => x.Type == "Cat");
    }
}

public class DogValidator: AbstractValidator<AnimalModel>
{
    public DogValidator()
    {
        RuleFor(x => x.Color).Equal("Black");
        RuleFor(x => x.NumberOfLegs).Equal(2);
        RuleFor(x => x.HasWings).Equal(false);
    }
}

Any help is appreciated!

  • 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-06-16T05:06:42+00:00Added an answer on June 16, 2026 at 5:06 am

    I don’t think this is possible using the When method unless you’re validating a “child” model.

    However, the calling code (e.g. your controller) could instead invoke the appropriate validator. Here’s a simplified example:

    public ActionResult SomeAction(AnimalModel model)
    {    
        ModelState.Clear();
    
        if (model.Type == "Dog")
            model.ValidateModel(new DogFullValidator(), ModelState);
        else if (model.Type == "Cat")
            model.ValidateModel(new CatFullValidator(), ModelState);
    
        // etc.
    }
    

    The above example uses a simple extension method to call the fluent validation:

    public static class ValidationExtensions
    {
        public static ModelStateDictionary ValidateModel<TModel, TValidator>(this TModel model, TValidator validator, ModelStateDictionary modelState)
            where TModel : class
            where TValidator : AbstractValidator<TModel>
        {
            var result = validator.Validate(model);
    
            result.AddToModelState(modelState, string.Empty);
    
            return modelState;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have two problem in this Case : I want to pass a JSON
I have the following code (in this case for two images - but in
I would like to validate some objects. The validation has two parts: validation whether
I have an object that handles the logging in of users. This object has
I am trying to validate two textboxes based on the URL that has been
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have this use case of an xml file with input like Input: <abc
I have this test case def setUp(self): self.user = User.objects.create(username=tauri, password='gaul') def test_loginin_student_control_panel(self): c
I have this SQL query select case when AllowanceId is null then 2 else
i have this table in UTF8 (collation is utf8_bin in case it matters). I

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.