Submitting a form with validation from outside the form

Does anyone know if I can use JS.dispatch or JS.exec to submit a form, but also have the constraint validations run.

I can trigger a form submit with: JS.dispatch("submit", to: "#form-id"), but that will bypass the client side validations.

Ideally what I need is a way to call requestSubmit on the form. Because that acts as if you’d press the button and so client-side validations are run.

While writing this I figured out a low effort way to implement this:

add this to your app.js:

window.addEventListener("app:requestSubmit", (event) => {
  const form = event.target;
  form.requestSubmit();
});

and then trigger JS.dispatch("app:requestSubmit", to: "#form-1")

1 Like