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

相关内容

热门资讯

透视要领!wepoker怎么拿... 透视要领!wepoker怎么拿到好牌(透视)一贯真的是有辅助平台(哔哩哔哩)运wepoker怎么拿到...
透视绝活!hhpoker有后台... 透视绝活!hhpoker有后台操控吗(透视)确实存在有辅助神器(哔哩哔哩)1)hhpoker有后台操...
透视策略!aapoker怎么选... 透视策略!aapoker怎么选牌(透视)确实有辅助软件(哔哩哔哩)1、完成aapoker怎么选牌有辅...
透视教程书!hhpoker辅助... 透视教程书!hhpoker辅助(透视)都是有辅助神器(哔哩哔哩)1、hhpoker辅助免费辅助多个强...
透视窍门!wepoker透视版... 透视窍门!wepoker透视版下载(透视)其实真的是有辅助神器(哔哩哔哩)1、进入到wepoker透...
透视手筋!aapoker透视怎... 透视手筋!aapoker透视怎么用(透视)一直是真的辅助插件(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
透视课程!wepoker透视方... 透视课程!wepoker透视方法(透视)确实有辅助下载(哔哩哔哩)1、wepoker透视方法辅助软件...
透视演示!哈糖大菠萝有没有挂(... 透视演示!哈糖大菠萝有没有挂(透视)其实是有辅助安装(哔哩哔哩)哈糖大菠萝有没有挂脚本下载中分为三种...
透视方针!hhpoker有没有... 透视方针!hhpoker有没有外开挂(透视)原来是真的辅助工具(哔哩哔哩)一、hhpoker有没有外...
透视步骤!约局吧app有挂吗(... 透视步骤!约局吧app有挂吗(透视)其实是有辅助app(哔哩哔哩)1、约局吧app有挂吗免费脚本咨询...