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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:28:51+00:00 2026-06-17T21:28:51+00:00

I am having trouble performing data type validation which is dependent on another field.

  • 0

I am having trouble performing data type validation which is dependent on another field. Most of the examples I found here are for making a field required or not based on a value of another field (MaidenName will be required only if IsMarried is true).

My Model

public class AttributeValuesModel
{
    public IList<AttributeModel> Values {get; set;}
}

public class AttributeModel
{
    [Required]
    public string AttributeName {get; set;}

    [Required]
    public string AttributeValue {get; set;}

    [Required]
    public int DataTypeId {get; set;}
}

What I would like to do is to validate the user input for AttributeValue based on the value of DataTypeId. For clarity, the value of DataTypeId is known before I even show the view to the user.

    //Possible values for DataTypeId are 
    //1 for decimal
    //2 for dates
    //3 for integer

Is this possible?

  • 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-17T21:28:53+00:00Added an answer on June 17, 2026 at 9:28 pm

    It’s not so hard to roll your own validation attribute. I have implemented one some time ago. It checks whether value of other property is smaller than property that is decorated with this attribiute:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited=true)]
    public class SmallerThanAttribute : ValidationAttribute
    {
        public SmallerThanAttribute(string otherPropertyName)
        {
            this.OtherPropertyName = otherPropertyName;
        }
    
        public string OtherPropertyName { get; set; }
        public string OtherPropertyDisplayName { get; set; }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return IsValid(OtherPropertyName, value, validationContext);
        }
    
        private ValidationResult IsValid(string otherProperty, object value, ValidationContext validationContext)
        {
            PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(otherProperty);
    
            if (otherPropertyInfo == null)
            {
                throw new Exception("Could not find property: " + otherProperty);
            }
    
            var displayAttribute = otherPropertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;
    
            if (displayAttribute != null && OtherPropertyDisplayName == null)
            {
                OtherPropertyDisplayName = displayAttribute.GetName();
            }
    
            object otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
    
            var smaller = (IComparable) value;
            var bigger = (IComparable) otherPropertyValue;
    
            if (smaller == null || bigger == null)
            {
                return null;
            }
    
            if (smaller.CompareTo(bigger) > 0)
            {
                return new ValidationResult(string.Format(ValidatorResource.SmallerThan, validationContext.DisplayName, OtherPropertyDisplayName));
            }
    
            return null;
        }
    }
    

    There is one gotcha. Error message format is defined in resource class property (ValidatorResource.SmallerThan), so it’s not pluggable — I didn’t need this. However, I think it still could be a good starting point for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
I am having trouble optimizing Hibernate queries to avoid performing joins or secondary selects.
I am having trouble in performing some activity when a call interrupt comes on
I am having some trouble performing the following operation.. http://www.google.com --> www.google.com/ https://google.com -->
I'm having trouble performing a cherry-pick. On my local machine, I'm currently on my
I am new to Python & xlrd and I am having trouble performing a
Having trouble with the examples for the .RowStart method. Comparing the 2 syntaxes: http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax/
I'm decent with regexes but stumped here. I'm having trouble with group 2 ,
I'm having trouble performing a search operation in the background, and displaying the results
I am having trouble retrieving some data out of an LDAP attribute in PHP.

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.