What would prevent the OnSubmit handler for an EditForm from firing?
I have a test app that exhibits an issue whereby the OnSubmit callback for an EditForm does not always fire when the submit button is clicked. I am trying to find out if the behavior shown in this test app is expected or if it is a bug.
It seems that if a required form field is blanked out and then the submit button is clicked (i.e. without navigating out of the form field), the UI validation is completed but the OnSubmit handler is not invoked.
Note: my team's application makes use of custom validations, so we use OnSubmit within EditForm rather than OnValidSubmit and OnInvalidSubmit.
Here is a video capture of the test page:
Here is the test page code:
@page "/FormTest"
@using System.ComponentModel.DataAnnotations;
<div class="mb-3">
<h2>Simple Form Test</h2>
</div>
<div class="mb-3">
The submit handler has been invoked <span>@_submitCount</span> times.
</div>
<div>
<EditForm Model="_model" OnSubmit="HandleSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="w-50">
<label for="title">Title</label>
<InputText @bind-Value=_model.Title id="title" class="form-control" />
<ValidationMessage For="@(() => _model.Title)" />
</div>
<div class="mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</EditForm>
</div>
@code {
public class MyModel
{
[Required]
public string Title { get; set; } = "A Title";
}
MyModel _model = new();
EditContext? _editContext;
int _submitCount = 0;
protected override void OnInitialized()
{
_editContext = new EditContext(_model);
_submitCount = 0;
base.OnInitialized();
}
void HandleSubmit(EditContext editContext)
{
_submitCount++;
var isValid = editContext.Validate();
}
}