Skip to content

Instantly share code, notes, and snippets.

@FluentGuru
Created June 6, 2019 19:24
Show Gist options
  • Save FluentGuru/1d7c6af745906871332a9ac4dd14954c to your computer and use it in GitHub Desktop.
Save FluentGuru/1d7c6af745906871332a9ac4dd14954c to your computer and use it in GitHub Desktop.
This extension method is a workaround to manually trigger a submit events of an EditForm component. Taken from EditForm.HandleSubmitAsync() private method.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Forms;
namespace FluentGuru.AspNetCore.Components.Forms
{
public static class EditFormExtensions
{
public static async Task SubmitAsync(this EditForm form)
{
if (form.OnSubmit.HasDelegate)
{
// When using OnSubmit, the developer takes control of the validation lifecycle
await form.OnSubmit.InvokeAsync(form.EditContext);
}
else
{
// Otherwise, the system implicitly runs validation on form submission
var isValid = form.EditContext.Validate(); // This will likely become ValidateAsync later
if (isValid && form.OnValidSubmit.HasDelegate)
{
await form.OnValidSubmit.InvokeAsync(form.EditContext);
}
if (!isValid && form.OnInvalidSubmit.HasDelegate)
{
await form.OnInvalidSubmit.InvokeAsync(form.EditContext);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment