Created
June 6, 2019 19:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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