Home >Backend Development >C++ >How to Implement Conditional Validation in ASP.NET Core using IValidatableObject?

How to Implement Conditional Validation in ASP.NET Core using IValidatableObject?

DDD
DDDOriginal
2025-01-28 11:46:10667browse

How to Implement Conditional Validation in ASP.NET Core using IValidatableObject?

In ASP.NET CORE, use ivalidatableObject to implement conditional verification

IvalidatableObject interface is a practical tool for verifying the ASP.NET core object. It allows you to define custom verification rules that can compare attributes or process conditions verification scenarios.

A common case of the interface is to verify certain attributes only under specific conditions. For example, the following code fragment demonstrates an object that can be verified only when the setting logo enable is set:

This method is not the most suitable in all cases, because it needs to check the enable logo in the value method and manually return the effective results.
<code class="language-csharp">public class ValidateMe : IValidatableObject
{
    [Required]
    public bool Enable { get; set; }
    [Range(1, 5)]
    public int Prop1 { get; set; }
    [Range(1, 5)]
    public int Prop2 { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (!this.Enable)
        {
            // 返回有效结果
            yield break;
        }
        else
        {
            // 检查Prop1和Prop2是否满足其范围要求,并相应地返回结果
            if (this.Prop1 < 1 || this.Prop1 > 5)
            {
                yield return new ValidationResult("Prop1必须在1到5之间", new[] { "Prop1" });
            }
            if (this.Prop2 < 1 || this.Prop2 > 5)
            {
                yield return new ValidationResult("Prop2必须在1到5之间", new[] { "Prop2" });
            }
        }
    }
}</code>

Another method is to use Validator.try ValidateProperty () method in the value method. This method allows you to specify verification for specific attributes:

In this case, only when the verification fails, the validator.tryvalidateProperty () method will add the verification results to the Results set. By using this method, you can verify the attributes conditionally according to the object of the object to make your verification logic more flexible and easier to maintain. Note that is added here to more clearly point out the wrong attributes.
<code class="language-csharp">public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var results = new List<ValidationResult>();
    if (this.Enable)
    {
        Validator.TryValidateProperty(this.Prop1,
            new ValidationContext(this, null, null) { MemberName = "Prop1" },
            results);
        Validator.TryValidateProperty(this.Prop2,
            new ValidationContext(this, null, null) { MemberName = "Prop2" },
            results);

        // 其他随机测试
        if (this.Prop1 > this.Prop2)
        {
            results.Add(new ValidationResult("Prop1必须大于Prop2", new[] { "Prop1", "Prop2" }));
        }
    }
    return results;
}</code>

The above is the detailed content of How to Implement Conditional Validation in ASP.NET Core using IValidatableObject?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn