MVC ValidationAttribute 驗證一個字段必須大於另外一個字段

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)]
    public class MinFieldAttribute : BaseCompareAttribute
    {
        public string MinValueField { get; set; }
        private string MinDisplayName { get; set; }

        public MinFieldAttribute(Type operandType, string minValueField,string minDisplayName) : base(operandType, minValueField, () => LocalizationExtensions.T("Rundong.Infrastructure.MinFieldAttribute.ErrorMsg", "{0}不能小於{1}"))
        {
            MinValueField = minValueField;
            MinDisplayName = minDisplayName;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var minPropertyInfo = validationContext.ObjectType.GetProperty(MinValueField);
            if (minPropertyInfo == null)
            {
                return new ValidationResult(string.Format(CultureInfo.CurrentCulture, LocalizationExtensions.T("Rundong.Infrastructure.MinFieldAttribute.NotExist", "{0}不存在"), MinValueField));
            }

            var minPropertyValue = minPropertyInfo.GetValue(validationContext.ObjectInstance, null);
            var compareValue = (IComparable) value;
            if (value!=null&& compareValue.CompareTo(minPropertyValue)<0)
            {
                if (MinDisplayName == null)
                {
                    MinDisplayName = ModelMetadataProviders.Current.GetMetadataForProperty(
                        () => validationContext.ObjectInstance, validationContext.ObjectType, MinValueField).GetDisplayName();
                }
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
            return null;
        }


        public override string FormatErrorMessage(string name)
        {
            return string.Format(ErrorMessageString, name, MinDisplayName??MinValueField);
        }

    }

  

public class MinFieldValidationMetadata<TValueType> : ModelValidationMetadata where TValueType : struct
    {
        public string MinValueField { get; set; }
        protected override ModelValidator CreateValidatorCore(ExtendedModelMetadata modelMetadata,
            ControllerContext context)
        {
            var type = typeof(TValueType?).GetUnderlyingType();
            var minModelMetadata=  modelMetadata.Properties.FirstOrDefault(a => a.PropertyName == MinValueField);
            var minDisplayName = minModelMetadata!=null ? minModelMetadata.GetDisplayName() : ModelMetadataProviders.Current.GetMetadataForProperty(() => null, modelMetadata.ContainerType, MinValueField).GetDisplayName();
            var attribute = new MinFieldAttribute(type, MinValueField, minDisplayName);
            return new BaseCompareAttributeAdapter(modelMetadata, context, attribute){MinValueField = MinValueField };
        }
    }

  經過重寫IsValid方向添加自定義驗證,MinValueField爲須要比較的字段名,MinDisplayName是顯示的名字。BaseCompareAttribute記錄的構造函數第三個參數爲重寫FormatErrorMessage函數,以及提供比較值的類型轉換git

 

具體是參考微軟兩個字段比較是否相同https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/CompareAttribute.csgithub

相關文章
相關標籤/搜索