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、微信广东雀神挂件辅助透视辅助软件...
发现一款!微信小程序功夫川辅助... 发现一款!微信小程序功夫川辅助,吉祥填大坑底牌图片,详细app(有人有挂);1、这是跨平台的微信小程...
一分钟揭秘!川南欢乐碰辅助,佛... 一分钟揭秘!川南欢乐碰辅助,佛手在线大菠萝可以作弊码,详细软件(有挂工具)1、全新机制【佛手在线大菠...
每日必看!九江讨赏辅助,维系小... 每日必看!九江讨赏辅助,维系小程序雀神挂件,详细插件(果真有挂)1、维系小程序雀神挂件辅助器安装包、...
技巧知识分享!欢乐达人暗堡辅助... 技巧知识分享!欢乐达人暗堡辅助,微信途游四川辅助器,详细神器(果真有挂)1、进入到微信途游四川辅助器...
记者爆料!科乐辅助,开心酷跑破... 记者爆料!科乐辅助,开心酷跑破解版,详细技巧(有挂解密)1、完成开心酷跑破解版辅助器v3.3的残局,...
重大通报!边锋透视器,微信小程... 重大通报!边锋透视器,微信小程序微乐辅助免费,详细软件(有挂细节)运微信小程序微乐辅助免费辅助工具,...
一分钟了解!爱玩联盟辅助软件视... 一分钟了解!爱玩联盟辅助软件视频讲解,潮汕汇软件辅助,详细方法(有挂总结)暗藏猫腻,小编详细说明爱玩...
一分钟了解!四川游戏辅助器,小... 一分钟了解!四川游戏辅助器,小程序能不能开挂,详细器(有挂细节)1、起透看视 小程序能不能开挂辅助软...
玩家必看攻略!麻友圈安庆版插件... 玩家必看攻略!麻友圈安庆版插件,川南小闲辅助器,详细app(证实有挂)暗藏猫腻,小编详细说明麻友圈安...