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

相关内容

热门资讯

透视辅助!uupoker透视(... 透视辅助!uupoker透视(透视)七千在线辅助(果然是有辅助插件)-哔哩哔哩1、下载好七千在线辅助...
透视肯定!wpk德州局透视(透... 透视肯定!wpk德州局透视(透视)微乐小程序辅助免费(一贯存在有辅助神器)-哔哩哔哩1、完成微乐小程...
于此同时!德普之星私人局辅助免... 于此同时!德普之星私人局辅助免费(透视)hhpoker作必弊实战视频(确实真的是有辅助插件)-哔哩哔...
为了进一步!wepoker辅助... 为了进一步!wepoker辅助真的假的(透视)微信黑科技辅助神器(竟然真的有辅助神器)-哔哩哔哩1、...
透视美元局!wepoker安装... 透视美元局!wepoker安装教程(透视)三哥玩摆头辅助(总是真的是有辅助软件)-哔哩哔哩1、打开软...
透视私人局!aapoker透视... 透视私人局!aapoker透视怎么用(透视)上饶打炸辅助(切实真的有辅助安装)-哔哩哔哩1、每一步都...
据悉!wpk私人辅助(透视)福... 据悉!wpk私人辅助(透视)福建啊当大菠萝有辅助么(一直真的是有辅助工具)-哔哩哔哩1、进入到福建啊...
据公告内容!wejoker黑侠... 据公告内容!wejoker黑侠辅助器(透视)吉利比鸡辅助(竟然真的是有辅助插件)-哔哩哔哩1、让任何...
在玩家背景下!pokerwor... 在玩家背景下!pokerworld破解版下载(透视)海豹大厅超级三加一辅助(本来是真的辅助平台)-哔...
透视ai代打!wepoker免... 透视ai代打!wepoker免费脚本弱密码(透视)约战竞技场辅助软件(竟然真的有辅助平台)-哔哩哔哩...