ASP.NETMVC:基于两个字段长度的DataAnnotation自定义验证方法
创始人
2024-11-12 05:30:16
0
  1. 首先,在需要自定义验证的Model中,添加需要验证的两个属性。

public class MyModel { public string Field1 { get; set; }

public string Field2 { get; set; }

}

  1. 接着,定义一个新的自定义ValidationAttribute类,命名为StringLengthComparisonAttribute,并继承ValidationAttribute类。

public class StringLengthComparisonAttribute : ValidationAttribute { private readonly string _dependentProperty; private readonly ComparisonOperator _comparisonOperator; private readonly int _dependentPropertyValue;

public StringLengthComparisonAttribute(string dependentProperty, ComparisonOperator comparisonOperator, int dependentPropertyValue)
{
    _dependentProperty = dependentProperty;
    _comparisonOperator = comparisonOperator;
    _dependentPropertyValue = dependentPropertyValue;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    var dependentProperty = validationContext.ObjectType.GetProperty(_dependentProperty);
    if (dependentProperty == null)
    {
        return new ValidationResult($"Unknown property {_dependentProperty}");
    }

    var dependentPropertyValue = dependentProperty.GetValue(validationContext.ObjectInstance, null);

    if (!int.TryParse(dependentPropertyValue.ToString(), out int intDependentPropertyValue))
    {
        return new ValidationResult($"Property {_dependentProperty} is not of type int");
    }

    if (value == null)
    {
        return ValidationResult.Success;
    }

    var valueString = value.ToString();

    var dependentPropertyValueString = dependentPropertyValue.ToString();
    
    if(_comparisonOperator == ComparisonOperator.GreaterThan) 
    {
        if (valueString.Length > intDependentPropertyValue)
        {
            return new ValidationResult($"The length of {validationContext.DisplayName} must be greater than the length of {_dependentProperty}");
        }
    } 
    else if(_comparisonOperator == ComparisonOperator.LessThan) 
    {
        if (valueString.Length < intDependentPropertyValue)
        {
            return new ValidationResult($"The length of {validationContext.DisplayName} must be less than the length of {_dependentProperty}");
        }
    }
    
    return ValidationResult.Success;
}

}

在StringComparisonOperator枚举中定义比较运算符。

public enum ComparisonOperator { LessThan, GreaterThan }

  1. 最后,在需要验证的属性

相关内容

热门资讯

分享开挂内幕!九九联盟辅助,奇... 分享开挂内幕!九九联盟辅助,奇迹手游辅助(总是是真的有挂)奇迹手游辅助辅助器是一种具有地方特色的麻将...
分享认知!小唐家乐园辅助器,w... 分享认知!小唐家乐园辅助器,we poker插件,大神讲解(有挂讲解);1、这是跨平台的小唐家乐园辅...
必备攻略!欢乐达人暗堡破解,光... 必备攻略!欢乐达人暗堡破解,光明大厅微信链接辅助试用(果然是真的有挂)1)光明大厅微信链接辅助试用有...
总算了解!小逸碰胡辅助器,德州... 总算了解!小逸碰胡辅助器,德州私人局脚本,细节揭秘(有挂功能);人气非常高,ai更新快且高清可以动的...
一起来探讨!科乐填大坑作弊码,... 一起来探讨!科乐填大坑作弊码,老友广东潮汕麻雀(果然真的有挂)1.老友广东潮汕麻雀 选牌创建新账号,...
新手必备!威信茶馆大贰破解,u... 新手必备!威信茶馆大贰破解,uupoker透视,总结教程(今日头条);相信小伙伴都知道这个威信茶馆大...
揭秘攻略!闲来透视辅助器最新版... 揭秘攻略!闲来透视辅助器最新版,欢乐联盟免费辅助(好像是真的有挂)1、完成闲来透视辅助器最新版有辅助...
一分钟揭秘!金虎爷有挂吗,we... 一分钟揭秘!金虎爷有挂吗,we-poker正规吗,曝光教程(有挂猫腻);1、点击下载安装,金虎爷有挂...
三分钟了解!内蒙博乐填大坑辅助... 三分钟了解!内蒙博乐填大坑辅助,雀神广东定制插件辅助(确实是有挂)在进入雀神广东定制插件辅助软件靠谱...
总算清楚!潮汕雀友会辅助,we... 【福星临门,好运相随】;总算清楚!潮汕雀友会辅助,wepoker开辅助能查到吗,微扑克教程(有挂解密...