public override bool ShouldSkipValidation(ControllerContext controllerContext, ModelBindingContext bindingContext, ModelMetadata propertyMetadata, ModelValidationResult validationResult)
        {
            PropertyInfo modelPropInfo = bindingContext.ModelType.GetProperty(_propertyName);

            if (modelPropInfo == null)
            {
                throw new InvalidOperationException("Could not find property " + _propertyName + " on type " + bindingContext.ModelType.Name);
            }

            object modelPropValue = modelPropInfo.GetValue(bindingContext.Model, null);
            var    result         = PropertyValidationComparisonUtil.Compare(modelPropValue, _comparisonType, _targetValue);

            return(!result.HasValue || result.Value);
        }
コード例 #2
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(_propertyName);

            if (property == null)
            {
                return(new ValidationResult("PropertyName is not specified."));
            }

            object targetValue = property.GetValue(validationContext.ObjectInstance, null);

            var comparisonResult = PropertyValidationComparisonUtil.Compare(value, _comparisonType, targetValue);

            if (!comparisonResult.HasValue || comparisonResult.Value)
            {
                return(null);
            }

            return(new ValidationResult(FormatErrorMessage(validationContext.DisplayName)));
        }