Using Lenses for form validation - is there a need...
# http4k
z
Using Lenses for form validation - is there a need to redirect the user back to the form and display errors? If so, how would you do it? I looked through the documents and had an invalid form body, but wasn't sure how to redirect a user back to the sign up page with the errors in the handlebars template from the invalidformbody
d
it depends really - you can use the "feedback" version of the validator in the form to collect the errors, then either return the form template populated with the errors, or redirect back to the "display form" endpoint with the errors in the request. I'd probably just go with the first one. This is where something like HTMX or Datastar comes in really handy because you can have the form contents loaded from a particular set of endpoints with the same path but with POST/GET which both use the same template under the hood... this keeps the logic and state for the form localised (and would be my go-to option)
🙌 1
z
Thank you. I ditched the form and am using handlebars templating since it is what I am familiar with - HTMX seems pretty neat from the few other repositories I've browsed but I'm trying to build a few thing with http4k before I switch my frontend template to something else
c
I did some work on this as part of Invirt framework. https://invirt.dev/docs/framework/forms/form-validation You're probably not looking for yet another framework (and Invirt only uses Pebble templates) but you can have a look at the code for some of the form handling and validation for inspiration. More specifically, handling complex forms à la Spring MVC, as well as validating (again, with another framework) and responding based on that outcome.
👀 1