9. Form Validation
Validation usually is only considered safe to do on the server. While client-side validation can limit the number of requests sent to your server for processing, it cannot guarantee that a malicious party hasn't tampered with the code, bypassing clientside validation. In addition, the complexity of some forms may make it difficult, if not impossible, to duplicate the same behavior on both the server and the client.
HTMX, in concert with ASP.NET Core's validation, can help you build validated forms without worrying about the need for any client-side validation.
In this sample, you'll submit the form and lean on ASP.NET Core to place the appropriate validation messages within the UI.
Note: The code found in Shared/_Form
is the first instance of Hyperscript in this project. We use it to disable the submit button so that users don't submit multiple requests.
Important Form
The request uses Task.Delay(Timespan.FromSeconds(1))
on the server so that you can see the loading spinner. Otherwise, HTMX + ASP.NET Core are way too fast.