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、用户打开应用后不用登录就可以直接使用...
针对!浙江游戏大厅修改数据&q... 针对!浙江游戏大厅修改数据"原来确实有辅助软件"(哔哩哔哩)所有人都在同一条线上,像星星一样排成一排...
反观!贪吃蛇辅助器怎么下载&q... 反观!贪吃蛇辅助器怎么下载"竟然真的是有辅助教程"(哔哩哔哩)贪吃蛇辅助器怎么下载能透视中分为三种模...
截至目前!潮汕激k辅助&quo... 截至目前!潮汕激k辅助"都是确实有辅助工具"(哔哩哔哩)1)潮汕激k辅助免费钻石:进一步探索潮汕激k...
今天上午!乐平包王攻略&quo... 今天上午!乐平包王攻略"果然存在有辅助工具"(哔哩哔哩)1、实时乐平包王攻略透视辅助更新:用户可以随...
更值得关注的是!决战卡五星游戏... 更值得关注的是!决战卡五星游戏辅助器"切实存在有辅助工具"(哔哩哔哩)决战卡五星游戏辅助器是不是有人...
教学辅助挂!科米台州麻将辅助&... 教学辅助挂!科米台州麻将辅助"竟然是有辅助器"(哔哩哔哩)1、上手简单,内置详细流程视频教学,新手小...
相较于以往!来几局是正规平台吗... 相较于以往!来几局是正规平台吗"其实确实有辅助工具"(哔哩哔哩)来几局是正规平台吗脚本下载中分为三种...
最终!朱雀开心罗松怎么开挂&q... 最终!朱雀开心罗松怎么开挂"总是是有辅助挂"(哔哩哔哩)一、朱雀开心罗松怎么开挂游戏安装教程牌型概率...
2026版规律!新西楚辅助器&... 2026版规律!新西楚辅助器"竟然确实有辅助器"(哔哩哔哩)1、每一步都需要思考,不同水平的挑战新西...