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

相关内容

热门资讯

透视了解!微扑克微乐辅助,wp... 透视了解!微扑克微乐辅助,wpk是真的还是假的-本来是真的有辅助软件(哔哩哔哩)透视方法中分为三种模...
透视总结!pokemmo手机脚... 透视总结!pokemmo手机脚本,德州局透视-竟然是真的有辅助方法(哔哩哔哩)1、实时pokemmo...
透视开挂!wpk插件,wpk模... 透视开挂!wpk插件,wpk模拟器是什么-其实真的是有辅助插件(哔哩哔哩)1、有没有辅助教程、有透视...
透视揭幕!wpk软件是真的吗,... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
透视辅助!云扑克有透视吗,拱趴... 透视辅助!云扑克有透视吗,拱趴大菠萝万能辅助器-原来是真的有辅助软件(哔哩哔哩)一、拱趴大菠萝万能辅...
透视曝光!wepoker辅助真... 透视曝光!wepoker辅助真的假的,wepoker辅助软件视频-确实一直都是有辅助工具(哔哩哔哩)...
透视详情!aapoker插件下... 透视详情!aapoker插件下载,aapoker辅助软件合法吗-果然一直总是有辅助插件(哔哩哔哩)1...
透视揭幕!wpk作弊,wpk透... 透视揭幕!wpk作弊,wpk透视辅助靠谱吗-确实是真的有辅助app(哔哩哔哩)所有人都在同一条线上,...
透视开挂!wpk作弊最怕三个东... 透视开挂!wpk作弊最怕三个东西,wpk私人局有透视吗-都是是有辅助软件(哔哩哔哩)1、起透看视 辅...
透视分享!wepoker有辅助... 透视分享!wepoker有辅助功能吗,we-poker是什么软件-确实是有辅助app(哔哩哔哩)1、...