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

相关内容

热门资讯

透视了解!pokermaste... 透视了解!pokermaster脚本,三哥玩摆头辅助,竟然真的是有挂(揭露必赢教程)-哔哩哔哩1、下...
透视网页版!wepoker免费... 透视网页版!wepoker免费透视脚本(透视)开挂辅助app(一贯是真的挂)-哔哩哔哩1、全新机制【...
透视了解!wpk真吗,hhpo... 透视了解!wpk真吗,hhpoker是正规的吗,窍要教程(切实有挂)-哔哩哔哩1、超多福利:超高返利...
透视网页版!we poker辅... 透视网页版!we poker辅助器下载(透视)开挂辅助攻略(真是是真的挂)-哔哩哔哩1、点击下载安装...
透视代打!wepoker有没有... 透视代打!wepoker有没有辅助,四川游戏家园辅助软件下载,一直存在有挂(详细软件教程)-哔哩哔哩...
透视实锤!德州圈脚本,hhpo... 透视实锤!德州圈脚本,hhpoker可以控制吗,手册教程(竟然真的是有挂)-哔哩哔哩1、任何hhpo...
透视挂!epoker底牌透视(... 透视挂!epoker底牌透视(透视)开挂辅助技巧(果然真的有挂)-哔哩哔哩1、透视挂!epoker底...
此事迅速冲上热搜!智星德州插件... 此事迅速冲上热搜!智星德州插件2024最新版,蜀渝牌乐汇修改器,本来真的是有挂(详细解密教程)-哔哩...
透视有挂!uupoker有透视... 透视有挂!uupoker有透视吗,hhpoker有没有辅助,指引教程(竟然有挂)-哔哩哔哩一、hhp...
透视小程序!wepoker有用... 透视小程序!wepoker有用吗(透视)开挂辅助app(果然存在有挂)-哔哩哔哩1、进入游戏-大厅左...