Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Validation attribute validates but does not show error message

$
0
0

I created a custom validation attribute:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]public class MinByteLengthAttribute(string lengthPropertyName) : ValidationAttribute{    public override bool RequiresValidationContext => true;    protected override ValidationResult IsValid(object value, ValidationContext validationContext)    {        var lengthProperty = validationContext.ObjectType.GetProperty(lengthPropertyName, BindingFlags.Instance | BindingFlags.Public) ?? throw new ArgumentException($"Property '{lengthPropertyName}' not found on type '{validationContext.ObjectType.Name}'.");        var minLength = (int)lengthProperty.GetValue(validationContext.ObjectInstance);        if (value is not byte[] bytes || bytes.Length < minLength)        {            var errorMessage = FormatErrorMessage(validationContext.DisplayName);            return new ValidationResult(errorMessage);        }        return ValidationResult.Success;    }}

And decorated a property with it:

public int ImageMinSize { get; set; } = 10000;[Required(ErrorMessageResourceType = typeof(Validation), ErrorMessageResourceName = "ImageRequired")][MinByteLength(nameof(ImageMinSize), ErrorMessageResourceType = typeof(Validation), ErrorMessageResourceName = "ImageTooSmall")]public byte[] Image { get; set; }

Validation works fine in the EditForm. For testing purposes I added:

<ValidationSummary></ValidationSummary>

And as standard I do have:

<ValidationMessage For="@(() => Model.Image)"/>

However, the validation message for MinByteLength is only shown in the summary and not below the image field. The validation message for 'Required' is shown in both places.

Am I missing something?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>