簡體   English   中英

C#數據注釋分組

[英]c# data annotations grouping

我希望嘗試將幾個驗證器加入同一驗證器中,以免丟失驗證器。

考慮,僅舉一個例子,我有一個要驗證的字符串類型的Id字段。 我需要應用幾個驗證器,例如RequiredMaxLengthMinLength和其他一些解析器。 現在,為了使其更加有趣,我想將EmailAddress驗證添加到我的所有Id字段,因此Im依賴於驗證器已經存在的事實,因此我只想將其添加到驗證組。

我有很多帶有Id的模型,我想制作一些新的驗證器,該驗證器實際上可以一起驗證幾件事,因此在字段上應用驗證器將更容易,也更正確。

找不到有關它的東西。

讓我們看一個例子:(忽略它將會失敗的事實。

[Required]
[EmailAddress]
[StringLength(6)]
[MinLength(5)]
[CustomA]
[CustomB]
public string Id { get; set; }

我只想寫

[IdValidator]
public string Id { get; set; }

而在其他地方,IdValidator會在所有我決定對其進行更改時驗證所有這些對象,並且更多/更少。 我希望更改僅在1個地方發生。

您可以創建自定義數據驗證屬性並實現所需的行為:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class IdentifierValidationAttribute : ValidationAttribute
{
    public int MinLength { get; set; }
    public int MaxLength { get; set; }

    public IdentifierValidationAttribute(int minLength, int maxLength)
    {
        MinLength = minLength;
        MaxLength = maxLength;
    }

    public override bool IsValid(object value)
    {
        var stringValue = value as string;
        if(string.IsNullOrEmpty(stringValue))
            return false;

        var length = stringValue.Length;

        if(length > MaxLength || length < MinLength)
            return false;

        return true;
    }
}

您也可以像下一個一樣制作復合屬性:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class IdentifierValidationAttribute : ValidationAttribute
{
    private readonly ValidationAttribute[] attributes;

    public IdentifierValidationAttribute(int minLength, int maxLength)
    {
        attributes = new ValidationAttribute[] { new EmailAddressAttribute(), new MinLengthAttribute(minLength), new MaxLengthAttribute(maxLength) };
    }

    public override bool IsValid(object value)
    {
        return attributes.All(a => a.IsValid(value));
    }
}

為什么不創建自己的分組屬性? 您可以將所需的屬性添加到_attributes數組。

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class GroupedValidationAttribute : ValidationAttribute, IClientValidatable
{
    private readonly ValidationAttribute[] _attributes;

    public GroupedValidationAttribute(int minLength, int maxLength)
    { 
        _attributes = new ValidationAttribute[]
        {
            new RequiredAttribute(),
            new EmailAddressAttribute(),
            new StringLengthAttribute(maxLength),
            new MinLengthAttribute(minLength),
            new CustomAAttribute(),
            new CustomBAttribute()
        };
    }

    public override bool IsValid(object value)
    {
        return _attributes.All(a => a.IsValid(value));
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        return _attributes
            .OfType<IClientValidatable>()
            .SelectMany(x => x.GetClientValidationRules(metadata, context));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM