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

How to create a custom validation attribute that works with Blazor SSR

$
0
0

I have a .NET 8 Blazor project where I have created some custom validation attributes. When using @rendermode InteractiveServer, they work as expected. However, when using SSR, the error messages aren't being displayed.

public class MinimumLength(int minLength) : ValidationAttribute{    private readonly string _defaultErrorMessage = $"Minimum length is {minLength}";    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)    {        var errorMessage = string.IsNullOrEmpty(ErrorMessage) ? _defaultErrorMessage : ErrorMessage;        if (value is string stringValue && stringValue.Length >= minLength)        {            return ValidationResult.Success;        }        return new ValidationResult(errorMessage);    }}

My form is an EditForm with an OnValidSubmit="HandleSubmit". If the custom validation fails, HandleSubmit isn't called, but the error message isn't displayed in <ValidationMessage />. The error message is only written in the <ValidationSummary />. However, I would like them to be written beneath the given field.

When using a built-in validation attributes, like [Required], everything works as expected.

Can anyone tell me what I need to change to achieve this behavior?

Thanks

/Morten


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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