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

相关内容

热门资讯

第四分钟教程!微乐小程序黑科技... 第四分钟教程!微乐小程序黑科技,微信小程序游戏破解微乐游戏(开挂)确实有挂(2026)1、任何微乐小...
据目击者称!微乐小程序黑科技,... 据目击者称!微乐小程序黑科技,微乐麻辣自建房辅助视频(透视)确实真的是有挂(哔哩哔哩)1、许多玩家不...
六分钟辅助!微乐小程序黑科技,... 六分钟辅助!微乐小程序黑科技,微信小程序微乐辅助器破解版(开挂)一贯是有挂(2026)1、进入到微乐...
备受关注的!微乐小程序黑科技,... 备受关注的!微乐小程序黑科技,小程序微乐陕西挖坑辅助器(透视)其实存在有挂(哔哩哔哩)1、金币登录送...
第1分钟工具!微乐小程序黑科技... 第1分钟工具!微乐小程序黑科技,广西微乐小程序控制器(开挂)竟然是有挂(2026)1、每一步都需要思...
现有说明如下!微乐小程序黑科技... 现有说明如下!微乐小程序黑科技,微信微乐有挂没有(透视)本来存在有挂(哔哩哔哩)1、任何微信微乐有挂...
5分钟插件!微乐小程序免费黑科... 5分钟插件!微乐小程序免费黑科技,微信小程序微乐辅助免费(开挂)总是是有挂(2026)1、许多玩家不...
透视黑科技!微乐小程序黑科技,... 透视黑科技!微乐小程序黑科技,微乐小程序免费黑科技下载方法(透视)竟然存在有挂(哔哩哔哩)1、起透看...
三分钟开挂!微乐小程序黑科技,... 三分钟开挂!微乐小程序黑科技,微乐内蒙古自建房脚本免费下载(开挂)都是有挂(2026)微乐小程序黑科...
现有关情况通报如下!微乐小程序... 现有关情况通报如下!微乐小程序黑科技,收i就微乐小程序游戏破解器(透视)果然真的是有挂(哔哩哔哩)暗...