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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:24:47+00:00 2026-05-27T06:24:47+00:00

Consider I have a these two properties: public class Test { [Required(ErrorMessage = Please

  • 0

Consider I have a these two properties:

public class Test
{
    [Required(ErrorMessage = "Please Enetr Age")]
    public System.Int32 Age { get; set; }
    [Required(ErrorMessage = "Choose an option")]
    public System.Boolean IsOld { get; set; }
}

When the user enters for example 15 for Age and choose “Yes” for IsOld, I return an exception that correct Age or IsOld. I’ve used CustomValidation for it, but because my my validation must be static I can’t access to other properties. How can I do this by a DataAnnotation?

  • 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-27T06:24:47+00:00Added an answer on May 27, 2026 at 6:24 am

    You can add data annotations (Custom Validator) to the class itself. In the isValid method of your validation attribute you should then be able to cast the object and test the values that need to be fulfilled.

    Example:

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel.DataAnnotations;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // setup test object
                Test t = new Test() { Age = 16, IsOld = true };
                // store validation results
                Collection<ValidationResult> validationResults = new Collection<ValidationResult>();
                // run validation
                if (Validator.TryValidateObject(t, new ValidationContext(t, null, null), validationResults, true))
                {
                    // validation passed
                    Console.WriteLine("All items passed validation");
                }
                else
                {
                    // validation failed
                    foreach (var item in validationResults)
                    {
                        Console.WriteLine(item.ErrorMessage);
                    }
                }
                Console.ReadKey(true);
            }
        }
    
        [TestValidation(ErrorMessage = "Test object is not valid")]
        public class Test
        {
            public int Age { get; set; }
            public bool IsOld { get; set; }
        }
    
        public class TestValidation : ValidationAttribute
        {
            public override bool IsValid(object value)
            {
                bool isValid = false;
                Test testVal = value as Test;
                if (testVal != null)
                {
                    // conditional logic here
                    if ((testVal.Age >= 21 && testVal.IsOld) || (testVal.Age < 21 && !testVal.IsOld))
                    {
                        // condition passed
                        isValid = true;
                    }
                }
                return isValid;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some doubts over how Enumerators work, and LINQ. Consider these two simple
Consider the following code snippet: class Test { public int Length{ get; set; }
Following are the two approaches: constructor with all the class properties Pros: I have
consider these two entities: /** @Entity */ class String { /** @Id @Column(type=integer) *
I have a queston regarding double-checked locking. Consider this example: public class Singleton {
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target
Consider I have the following interface: public interface A { public void b(); }
Consider I have a shared function:- Public Shared Function CalculateAreaFromRadius(ByVal radius As Double) As
Consider I have two arrays: $friends = Array('foo', 'bar', 'alpha'); $attendees = Array('foo', 'bar');
I have an array filled with instances of a custom class which contains two

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.