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

相关内容

热门资讯

黑科技辅助!智星德州菠萝偷偷看... 黑科技辅助!智星德州菠萝偷偷看功能,(智星德州菠萝开挂)原来真的有挂,专业教程(讲解)-哔哩哔哩;【...
黑科技辅助!悟空黑桃a辅助,(... 您好:悟空黑桃a辅助这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特...
黑科技辅助!德扑ai工具,(德... 黑科技辅助!德扑ai工具,(德扑好牌)原来真的有挂,揭秘教程(技巧)-哔哩哔哩;德扑ai工具AI智能...
黑科技辅助!aapoker有猫... 黑科技辅助!aapoker有猫腻,(AAPoKer真的假的)原来真的有挂,规律教程(教程)-哔哩哔哩...
黑科技辅助!aapoker有外... 黑科技辅助!aapoker有外挂,(AAPoKer靠谱)原来真的有挂,2025新版技巧(助手)-哔哩...
黑科技辅助!德扑智能ai,(德... 黑科技辅助!德扑智能ai,(德扑ai数据)原来真的有挂,2025教程(发现)-哔哩哔哩是一款可以让一...
黑科技辅助!wpk有长期盈利玩... 黑科技辅助!wpk有长期盈利玩家,(WPK统计)原来真的有挂,详细教程(方式)-哔哩哔哩;1、实时W...
黑科技辅助!Wepoke透明挂... 黑科技辅助!Wepoke透明挂,(WePoKe逻辑)原来真的有挂,攻略教程(有挂存在)-哔哩哔哩;亲...
黑科技辅助!wepoke辅助插... 黑科技辅助!wepoke辅助插件,(WePoKe展示)原来真的有挂,黑科技教程(有挂透视)-哔哩哔哩...
黑科技辅助!wepoke黑科技... 黑科技辅助!wepoke黑科技功能演示,(WePoKe能胜)原来真的有挂,安装教程(存在有挂)-哔哩...