コード例 #1
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // Get values
            var comparableValue = value as IComparable;
            IComparable comparableOtherValue;
            try {
                comparableOtherValue = validationContext.GetPropertyValue<IComparable>(this.OtherPropertyName);
            }
            catch (ArgumentException aex) {
                throw new InvalidOperationException("Other property not found", aex);
            }

            // Empty or noncomparable values are valid - let others validate that
            if (comparableValue == null || comparableOtherValue == null) return ValidationResult.Success;

            var compareResult = comparableValue.CompareTo(comparableOtherValue);
            if (compareResult == 1 || (this.AllowEqual && compareResult == 0)) {
                // This property is greater than other property or equal when permitted
                return ValidationResult.Success;
            }
            else {
                // This property is smaller or equal to the other property
                if (string.IsNullOrWhiteSpace(this.OtherPropertyDisplayName)) {
                    this.OtherPropertyDisplayName = validationContext.GetPropertyDisplayName(this.OtherPropertyName);
                }
                return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
            }
        }
コード例 #2
0
        public static bool IsCompareToType(this ValidationContext validationContext, object value, string compareToPropertyName, IComparableExtensions.CompareToType compareToType)
        {
            var thisValue  = value.As <object, IComparable>();
            var otherValue = validationContext.GetPropertyValue(compareToPropertyName).As <object, IComparable>();

            return(thisValue.IsCompareToType(otherValue, compareToType));
        }
コード例 #3
0
        public static int CompareToProperty(this ValidationContext validationContext, object value, string compareToPropertyName)
        {
            var currentValue    = value.As <object, IComparable>();
            var comparisonValue = validationContext.GetPropertyValue(compareToPropertyName).As <object, IComparable>();

            if (!ReferenceEquals(value.GetType(), comparisonValue.GetType()))
            {
                throw new ArgumentException("The properties types must be the same");
            }
            return(currentValue.CompareTo(comparisonValue));
        }
コード例 #4
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // Always succeed on empty value
            if (value == null) return ValidationResult.Success;

            // Get other property value
            var currentOtherValue = validationContext.GetPropertyValue(this.OtherPropertyName);

            // Compare to it
            if (object.Equals(this.OtherPropertyValue, currentOtherValue) == this.NegateCondition) return ValidationResult.Success;

            // Return error
            return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
        }
コード例 #5
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var comparableValue = value as IComparable;
            var comparableOtherValue = validationContext.GetPropertyValue<IComparable>(this.OtherPropertyName);

            // Empty or noncomparable values are valid - let others validate that
            if (comparableValue == null || comparableOtherValue == null) return ValidationResult.Success;

            var compareResult = comparableValue.CompareTo(comparableOtherValue);
            if (compareResult == 1 || (this.AllowEqual && compareResult == 0)) {
                // This property is greater than other property or equal when permitted
                return ValidationResult.Success;
            }
            else {
                // This property is smaller or equal to the other property
                if (string.IsNullOrWhiteSpace(this.OtherPropertyDisplayName)) this.OtherPropertyDisplayName = this.OtherPropertyName;
                return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
            }
        }