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. 最后,在需要验证的属性

相关内容

热门资讯

一分钟了解!aa poker辅... 一分钟了解!aa poker辅助,拱趴大菠萝作弊方法,线上教程(有挂透明)准备好在拱趴大菠萝作弊方法...
必备教程!安装不了wepoke... 1、必备教程!安装不了wepoker,wepoker插件程序激活码,必赢教程(有挂辅助);详细教程。...
热点推荐!拱趴大菠萝怎么开挂,... 热点推荐!拱趴大菠萝怎么开挂,德普之星辅助器,攻略教程(有挂教程)是一款可以让一直输的玩家,快速成为...
玩家必看分享!hhpoker透... 玩家必看分享!hhpoker透视方法,wepoker可以透视码,教你攻略(有挂辅助);支持2-10人...
透视模拟器"约局吧游... 透视模拟器"约局吧游戏挂"详细辅助爆料教程(可以提高运气软件);《WPK辅助透视》‌:支持手机实时对...
盘点一款!佛手大菠萝辅助,we... 盘点一款!佛手大菠萝辅助,wepoker辅助软件视频,wepoke教程(有挂技巧);小薇(透视辅助)...
透视安装"poker... 透视安装"pokerrrr2辅助"详细辅助攻略教程(私人局俱乐部可以进透视),支持语音通讯、好友开房...
玩家必备教程!德州私人局怎么透... 玩家必备教程!德州私人局怎么透视,wepoker破解游戏盒子,黑科技教程(有挂方法);小薇(透视辅助...
透视安卓版"poke... 透视安卓版"pokerworld修改器"详细辅助详细教程(有机器人方法);1分钟了解详细教程(微信 ...
重大推荐!we-poker靠谱... 重大推荐!we-poker靠谱吗,we-poker辅助器,必赢方法(有挂技巧);支持多人共享记分板与...