protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherProperty);

            if (otherPropertyInfo == null)
            {
                //TODO: Globalization
                return(new ValidationResult(String.Format(CultureInfo.CurrentCulture,
                                                          "Could not find a property named {0}.", OtherProperty)));
            }

            object otherValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);

            if (null == value && null == otherValue)
            {
                return(null);
            }

            if (null == value || null == otherValue)
            {
                return(null);
            }

            ValidationDataType valueType;

            if (!TypeDataTypeMap.TryGetValue(value.GetType(), out valueType))
            {
                return(null);                //we can't judge this pair as know nothing about them
            }

            if (Type != ValidationDataType.String)
            {
                return(CompareNonString(value, validationContext, otherPropertyInfo, otherValue,
                                        valueType));
            }
            else                 //Type is string
            {
                value      = Convert.ToString(value);
                otherValue = Convert.ToString(otherValue);
                if (String.IsNullOrWhiteSpace((string)value) ||
                    String.IsNullOrWhiteSpace((string)otherValue))
                {
                    return(null);
                }
                if (!Comparers[Operator]((IComparable)value, (IComparable)otherValue))
                {
                    TryToExtractOtherPropertyTitle(otherPropertyInfo);
                    return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
                }
            }
            return(null);
        }
        private ValidationResult CompareNonString(object value, ValidationContext validationContext,
                                                  PropertyInfo otherPropertyInfo, object otherValue, ValidationDataType valueType)
        {
            if (valueType == ValidationDataType.String)
            {
                var otherValueString = Convert.ToString(otherValue);
                if (String.IsNullOrWhiteSpace((string)value) || String.IsNullOrWhiteSpace(otherValueString))
                {
                    return(null);
                }
                try {
                    value      = Convert.ChangeType(value, DataTypeTypeMap[Type]);
                    otherValue = Convert.ChangeType(otherValue, DataTypeTypeMap[Type]);
                }
                catch {
                    return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
                }
            }
            else
            {
                ValidationDataType otherValueType;
                if (!TypeDataTypeMap.TryGetValue(otherValue.GetType(), out otherValueType))
                {
                    return(null);                    //we can't judge this pair as know nothing about them
                }

                if (valueType != Type || otherValueType != Type)
                {
                    try {
                        value      = Convert.ChangeType(value, DataTypeTypeMap[Type]);
                        otherValue = Convert.ChangeType(otherValue, DataTypeTypeMap[Type]);
                    }
                    catch (InvalidCastException) {
                        return(null);                        //we can't judge this pair as know nothing about them
                    }
                }

                if (!(value is IComparable) || !(otherValue is IComparable))
                {
                    TryToExtractOtherPropertyTitle(otherPropertyInfo);
                    return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
                }
            }
            if (!Comparers[Operator]((IComparable)value, (IComparable)otherValue))
            {
                TryToExtractOtherPropertyTitle(otherPropertyInfo);
                return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
            }
            return(null);
        }
Beispiel #3
0
        protected ValidationResult IsValidDataType(object value, ValidationContext context)
        {
            if (null == value)
            {
                return(null);
            }

            ValidationDataType valueType;

            if (!TypeDataTypeMap.TryGetValue(value.GetType(), out valueType))
            {
                return(new ValidationResult(FormatErrorMessage(context.DisplayName)));
            }

            if (valueType == Type)
            {
                return(null);
            }

            if (Type == ValidationDataType.String)
            {
                return(new ValidationResult(FormatErrorMessage(context.DisplayName)));
            }

            if (valueType != ValidationDataType.String)
            {
                return(new ValidationResult(FormatErrorMessage(context.DisplayName)));
            }

            if (String.IsNullOrEmpty((string)value))
            {
                return(null);
            }
            try {
                Convert.ChangeType(value, DataTypeTypeMap[Type]);
                return(null);
            }
            catch {
                return(new ValidationResult(FormatErrorMessage(context.DisplayName)));
            }
        }