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

相关内容

热门资讯

透视透视!wpk辅助器是真的吗... 透视透视!wpk辅助器是真的吗,wpk作弊最怕三个东西,必胜教程(一直存在有挂);1、打开软件启动之...
透视软件!hhpoker辅助软... 透视软件!hhpoker辅助软件,都是有挂(透视)第三方教程(有挂解密)1、hhpoker辅助软件透...
透视了解!wpk安卓下载辅助,... 透视了解!wpk安卓下载辅助,wpk是真的还是假的,攻略方法(一直是有挂)wpk是真的还是假的是一种...
透视私人局!wepoker有透... 透视私人局!wepoker有透视吗,好像是有挂(透视)wpk教程(有挂技巧);1、完成wepoker...
透视线上!有哪些免费的wpk作... 透视线上!有哪些免费的wpk作弊码,wpk辅助是什么,详细教程(其实是有挂);1、有哪些免费的wpk...
透视插件!wpk辅助购买,其实... 透视插件!wpk辅助购买,其实是有挂(透视)教你教程(有挂解说)wpk辅助购买是一种具有地方特色的麻...
透视私人局!wpk透视辅助,w... 透视私人局!wpk透视辅助,wpk辅助器是真的吗,介绍教程(其实有挂)所有人都在同一条线上,像星星一...
辅助透视!wepoker透视方... 辅助透视!wepoker透视方法,竟然真的有挂(透视)攻略教程(有挂详情);1、wepoker透视方...
透视教学!wpk有作弊吗,wp... 透视教学!wpk有作弊吗,wpk辅助哪里买,2025新版(切实真的有挂)1、每一步都需要思考,不同水...
透视透视!wepoker能不能... 透视透视!wepoker能不能透视,先前真的是有挂(透视)透明挂教程(有挂揭秘)1、起透看视 wep...