I am testing FluentValidation & Blazored.FluentValidation with ValueChanged and have found that the validation message works correctly when using Tab or clicking away from the field. However, when Enter key is used the validation message does not appear (flashes briefly).
@using Blazored.FluentValidation
@using FluentValidation
<EditForm Model="@_annotation">
<FluentValidationValidator @ref="_testFluentValidationValidator" />
<SfTextBox
Value="@_annotation.Name"
ValueChanged="ChangeValue"></SfTextBox>
<ValidationMessage For="@(() => _annotation.Name)" />
</EditForm>
@code {
private FluentValidationValidator? _testFluentValidationValidator;
private Annotation _annotation = new();
protected async Task ChangeValue(string newValue)
{
_annotation.Name = newValue;
var result = (await _testFluentValidationValidator!.ValidateAsync(options => options.IncludeRuleSets("TestNames")));
if (result)
{
await Task.Delay(100);
}
else
{
await Task.Delay(100);
}
}
public class AnnotationValidator: AbstractValidator<Annotation>
{
public AnnotationValidator()
{
RuleSet("TestNames", () =>
{
RuleFor(e => e.Name)
.NotEmpty().WithMessage("Please enter a name.")
.MinimumLength(3).WithMessage($"Name must be longer than 2 characters.");
});
}
}
public class Annotation
{
public string Name { get; set; }
}
}