码迷,mamicode.com
首页 > Web开发 > 详细

MVC ValidationAttribute 验证一个字段必须大于另一个字段

时间:2019-10-18 14:09:30      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:info   mes   ntc   bsp   ESS   net   creat   stack   显示   

[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函数,以及提供比较值的类型转换

 

具体是参考微软两个字段比较是否相同https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/CompareAttribute.cs

MVC ValidationAttribute 验证一个字段必须大于另一个字段

标签:info   mes   ntc   bsp   ESS   net   creat   stack   显示   

原文地址:https://www.cnblogs.com/Cyril-hcj/p/11697871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!