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

  • Home
  • SEARCH
  • 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 4077158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:30:55+00:00 2026-05-20T17:30:55+00:00

I am playing around with The Enterprise Library Validation Block. I have a class

  • 0

I am playing around with The Enterprise Library Validation Block. I have a class from a Linq To Entities edmx file I am using in an MVC project. I want to make sure that a Nullable DateTime is always later than a DateTime. I am using attributes in a metadata class to create a default ruleset. When attempting to validate with the PropertyComparisonValidator I get the Exception:

A validation attribute of type PropertyComparisonValidatorAttribute cannot be used to validate values.

I theorized that I couldn’t compare a Nullable type to a struct so I wrote the custom class below specifcally to get around that perceived problem. Still I get this exception:

A validation attribute of type NullableDateComparisonValidatorAttribute cannot be used to validate values.

Next I attempted Self Validation from the Enterprise Library and that failed to fire when validating. I am at a temporary standstill until I get this figured out. Please suggest a solution\workaround that isn’t too ugly.

using System;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace IdahoUtility
{
    public class NullableComparisonValidator<T> : Validator<Nullable<T>>
        where T: struct, IComparable
    {
        protected string propertyToCompare { get; set; }
        protected ComparisonOperator comparisionOperator { get; set; }
        protected T TargetProperty(object currentTarget)
        {
            if (null == currentTarget)
            {
                throw new ArgumentNullException("currentTarget");
            }

            Type t = currentTarget.GetType();
            PropertyInfo pInfo = t.GetProperty(propertyToCompare);
            object oValue = pInfo.GetValue(currentTarget, null);

            if (oValue.GetType() != typeof(T))
            {
                throw new InvalidOperationException(string.Format("Property compared must be a {0}!",typeof(T)));
            }

            return (T)oValue;
        }
        public NullableComparisonValidator(string PropertyToCompare, ComparisonOperator cmpOp)
            :base(null,null)
        {
            if (string.IsNullOrWhiteSpace(PropertyToCompare))
            {
                throw new ArgumentException("PropertyToCompare is Invalid!", "PropertyToCompare");
            }
            propertyToCompare = PropertyToCompare;
            comparisionOperator = cmpOp;
        }
        protected override void DoValidate(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            // ignore nulls
            if (null == objectToValidate)
            {
                return;
            }

            switch (comparisionOperator)
            {
                case ComparisonOperator.Equal:
                    DoValidateEqual(objectToValidate, currentTarget, key, validationResults);
                    break;
                case ComparisonOperator.GreaterThan:
                    DoValidateGreaterThan(objectToValidate, currentTarget, key, validationResults);
                    break;
                case ComparisonOperator.GreaterThanEqual:
                    DoValidateGreaterThanEqual(objectToValidate, currentTarget, key, validationResults);
                    break;
                case ComparisonOperator.LessThan:
                    DoValidateLessThan(objectToValidate, currentTarget, key, validationResults);
                    break;
                case ComparisonOperator.LessThanEqual:
                    DoValidateLessThanEqual(objectToValidate, currentTarget, key, validationResults);
                    break;
                case ComparisonOperator.NotEqual:
                    DoValidateNotEqual(objectToValidate, currentTarget, key, validationResults);
                    break;             
            }
        }

        private void DoValidateLessThanEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) > 0)
            {
                LogValidationResult(validationResults, string.Format("Should be less than or equal to {0}!", propertyToCompare), currentTarget, key);
            }
        }

        private void DoValidateLessThan(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) >= 0)
            {
                LogValidationResult(validationResults, string.Format("Should be less than {0}!", propertyToCompare), currentTarget, key);
            }
        }

        private void DoValidateGreaterThanEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) < 0)
            {
                LogValidationResult(validationResults, string.Format("Should be greater than or equal {0}!", propertyToCompare), currentTarget, key);
            }
        }

        private void DoValidateGreaterThan(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) <= 0)
            {
                LogValidationResult(validationResults, string.Format("Should be greater than {0}!", propertyToCompare), currentTarget, key);
            }
        }

        private void DoValidateEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) != 0)
            {
                LogValidationResult(validationResults, string.Format("Should be equal to {0}!", propertyToCompare), currentTarget, key);
            }
        }

        private void DoValidateNotEqual(Nullable<T> objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if (objectToValidate.Value.CompareTo(TargetProperty(currentTarget)) == 0)
            {
                LogValidationResult(validationResults, string.Format("Should not be equal to {0}!", propertyToCompare), currentTarget, key);
            }
        }

        protected override string DefaultMessageTemplate
        {
            get { return "{0}"; }
        }
    }
}
  • 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-20T17:30:56+00:00Added an answer on May 20, 2026 at 5:30 pm

    The problem had nothing to do with it being a nullable type. I ended up completely reworking the Validation. I removed the Metadata classes with attributes and moved my validation to configuration using the Enterprise Library tool. I was finally able to use a Property Comparison Validator to ensure that the end date followed the start date. The final snag was that I couldn’t use the default Rule Set. I am using this in an ASP.NET MVC web site and I was able to make client side validation work using the Enterprise Library. I am actually very pleased with the whole thing even if it is unclear to me why the original set up did not work.

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

Sidebar

Related Questions

Playing around in order to learn XSLT, I have the following XML file and
Was playing around a little bit with the TimeSpan class and I started wondering
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
iam playing around with OPEN CV from Intel with the associated PHP Extensions. Works
Playing around with generating text randomly with each page refresh using php. Is there
I have been playing around with various methods of making the Visitor pattern in
I've been playing around with Simple.Data and have run across something that I can't
I'm just playing around with some code but it seemed to have the specific
I'm playing around with networkx (graph library in python) and I found documentation saying
Playing around with Google Maps these days, with some directions. I have a map

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.