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

相关内容

热门资讯

红龙扑克辅助工具!红龙扑克有没... 1、红龙扑克辅助工具!红龙扑克有没有外卦挂,(红龙扑克)确实是真的有挂(详细辅助挂教程)。2、透视辅...
wepoke智能ai!wepo... 此外,数据分析德州(wepoke智能ai)辅助神器app还具备辅助透视行为开挂功能,通过对客户wep...
智星德州菠萝辅助器!德扑助手,... 智星德州菠萝辅助器!德扑助手,德扑ai软件总是是真的有挂(详细房间设置教程)是一款可以让一直输的玩家...
wepoke辅助有挂!wepo... wepoke辅助有挂!wepoke管理员(wepoke好牌)好像真的有挂(详细有挂教程)1、让任何用...
aapoker透明挂!aapo... aapoker透明挂!aapoker到底有挂吗,(aapoker模拟器)好像真的有挂(详细有挂教程)...
智星德州菠萝外卦挂!德扑之星的... 智星德州菠萝外卦挂!德扑之星的发牌是不是有问题,德扑网上设置其实是真的有挂(详细ai教程)准备好在智...
德扑工具!德扑牌型胜率,德扑自... 德扑工具!德扑牌型胜率,德扑自定义房间总是真的有挂(详细猫腻教程)1、每一步都需要思考,不同水平的挑...
德扑之星猫腻!德扑之星数据解析... 德扑之星猫腻!德扑之星数据解析,德扑之星解析真是是真的有挂(详细电脑教程);wpk透视辅助官方版是专...
微扑克辅助挂!微扑克发牌规律性... WePoker透视辅助工具核心要点解析‌,微扑克辅助挂!微扑克发牌规律性,(微扑克苹果版)确实是有挂...
微扑克系统发牌规律!微扑克小程... 此外,数据分析德州(微扑克系统发牌规律)辅助神器app还具备辅助透视行为开挂功能,通过对客户微扑克系...