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、许...
透视好友!wepoker辅助器... 透视好友!wepoker辅助器最新版本更新内容,hhpkoer辅助器(透视)分享教程(有挂方法)-哔...
据相关数据显示!四川游戏家园破... 据相关数据显示!四川游戏家园破解版,雀神山庄麻将辅助器,窍门教程(有挂详细)-哔哩哔哩1、四川游戏家...
事发当天!aa poker辅助... 事发当天!aa poker辅助包,we-poker正规吗(透视)解密教程(有挂规律)-哔哩哔哩1、每...
不少玩家反映!决战卡五星开挂方... 不少玩家反映!决战卡五星开挂方法,hhpoker一直输有挂吗,绝活教程(有挂方法)-哔哩哔哩1、许多...
推出新举措!pokemmo脚本... 推出新举措!pokemmo脚本辅助器下载,hhpoker是真的假的(透视)关于教程(有挂总结)-哔哩...
针对!新鸿狐辅助软件是真的吗,... 针对!新鸿狐辅助软件是真的吗,随意玩辅助软件,积累教程(揭秘有挂)-哔哩哔哩新鸿狐辅助软件是真的吗是...
透视最新!wepoker买脚本... 透视最新!wepoker买脚本靠谱吗,wepoker怎么买辅助(透视)普及教程(有挂头条)-哔哩哔哩...
于此同时!we-poker软件... 于此同时!we-poker软件,wpk俱乐部是真的吗(透视)总结教程(有挂分析)-哔哩哔哩wpk俱乐...
目前!越乡游义乌透视下载安卓,... 目前!越乡游义乌透视下载安卓,wepoker辅助分析器,经验教程(有挂神器)-哔哩哔哩1、超多福利:...