Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose form-level valid prop on client side for submission prevention? #35

Open
brophdawg11 opened this issue Jan 16, 2023 · 0 comments

Comments

@brophdawg11
Copy link
Owner

brophdawg11 commented Jan 16, 2023

Still thinking about whether this is needed, but right now when you submit to the server you get back a form-level valid prop. But since all of our validation is field-level on the client there's a not exactly an easy way to prevent submission on failed client side navigation. Arguably, this is less-common with real-time inline error messaging but it's still nice to have.

<FormProvider ...>
  <form onSubmit={(e) => {
    // what to check here?
    if (!valid) {
      e.preventDefault();
    }
  }}>
    ...
  </form>
</FormProvider>

If you're not using custom validations you could check directly via the DOM:

<form onSubmit={(e) => {
  let inputs = Array.from(e.currentTarget.elements);
  let isInvalid = inputs.some((e) => e.validity?.valid === false);
  if (isInvalid) {
    e.preventDefault();
  }
}}>

But that doesn't account for customValidations nor does it give you a way to disable a submit button when the form is invalid. So I think this is likely going to require something at the FormProvider level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant