Home >Backend Development >C++ >How to Implement Custom Validation for Combined Property Length in ASP.NET MVC?

How to Implement Custom Validation for Combined Property Length in ASP.NET MVC?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-16 19:32:11823browse

How to Implement Custom Validation for Combined Property Length in ASP.NET MVC?

Use data annotations to implement custom verification of combined attribute length in ASP.NET MVC

Custom validation properties provide flexibility when validating multiple properties simultaneously. In ASP.NET MVC, you can create custom validation properties using the ValidationAttribute base class and apply them to properties in the model using the [Validate] attribute.

Custom validation attributes for combined attribute lengths

To validate the combined length of multiple string properties, you can create a custom validation property like this:

<code class="language-csharp">public class CombinedMinLengthAttribute : ValidationAttribute
{
    public CombinedMinLengthAttribute(int minLength, params string[] propertyNames)
    {
        this.PropertyNames = propertyNames;
        this.MinLength = minLength;
    }

    public string[] PropertyNames { get; private set; }
    public int MinLength { get; private set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
        var values = properties.Select(p => p.GetValue(validationContext.ObjectInstance, null)).OfType<string>();
        var totalLength = values.Sum(x => x.Length) + Convert.ToString(value).Length;

        if (totalLength < MinLength)
        {
            return new ValidationResult(ErrorMessageString);
        }

        return ValidationResult.Success;
    }
}</code>

Use in model

To apply this validation to your model, you can decorate one of its properties with the [Validate] attribute:

<code class="language-csharp">public class MyViewModel
{
    [CombinedMinLength(20, "Bar", "Baz", ErrorMessage = "Foo, Bar和Baz属性的组合最小长度应大于20")]
    public string Foo { get; set; }
    public string Bar { get; set; }
    public string Baz { get; set; }
}</code>

This validation property ensures that the combined length of the Foo, Bar, and Baz properties is greater than or equal to the specified minimum length (20 in this example). If validation fails, an appropriate error message is displayed in the validation summary (as defined in the ErrorMessage parameter).

The above is the detailed content of How to Implement Custom Validation for Combined Property Length in ASP.NET MVC?. 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