question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I trigger validation with a ref?

See original GitHub issue

Description

I have a Material UI dialog with a Wizard/Funnel with multiple forms inside. Since Material Design demands the button to be on the bottom part of the dialog, there is no submit button inside any form.

const Page() {
  const formRef = useRef<HTMLFormElement>();

  const onSubmit = (payload: FirstFormValues | SecondFormValues | ThirdFormValues) => {
    // code to save each form state
  }

  const onNextStep = () => {
    formRef.current!.submit();  // <-- this submit does not trigger validation, but a redirect

    setCurrentStep(currentStep => currentStep + 1);
  }

  return (
    <Dialog>
      <DialogContent>
        <Funnel ref={formRef} currentStep={currentStep} />
      </DialogContent>  
      <DialogActions>
        <Button onClick={handleNextStep}>Next</Button>
      </DialogActions>
    </Dialog>
  );
}


const Funnel = forwardRef<HTMLFormElement, Props>(({ onSubmit }, formRef) => {
  return (
    <> 
      {step === 0 && <FirstForm ref={formRef} onSubmit={onSubmit} />}
      {step === 1 && <SecondForm ref={formRef} onSubmit={onSubmit} />}
      {step === 2 && <ThirdForm ref={formRef} onSubmit={onSubmit} />}
    </>
  );
})

const FirstForm = forwardRef<HTMLFormElement, Props>(({ onSubmit }, formRef) => {
  const { handleSubmit, register } = useForm()

  return (
    <form ref={formRef} onSubmit={handleSubmit(onSubmit)}>
      <input type="submit" value="Just to test" />
    </form>
  );
})

Clicking on the “Just to test” button correctly triggers the validation on the form. However, the call formRef.current!.submit() causes a page refresh.

What’s the right way of doing this?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
christopher-franciscocommented, Mar 16, 2020

@bluebill1049 no problem, you always help me so the least I can do is make it easier for you. I’m REALLY grateful man 😃 love this library

1reaction
christopher-franciscocommented, Mar 16, 2020

@bluebill1049 Sure thing. what does CSB stand for?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to trigger validation in formik after rendering?
When user click on the Id of a form, it will show up the form itself. The requirement is the errors should be...
Read more >
useForm - trigger - Simple React forms validation
Manually triggers form or input validation. This method is also useful when you have dependant validation (input validation depends on another input's ...
Read more >
Angular Reactive Forms: trigger validation on submit
Validating all fields on submit ... To validate all form fields, we need to iterate throughout all form controls: Object.keys(this.form.controls).
Read more >
Fixing Vuetify's Form Validation. Quite ... - Robert Mirabelle
You can still call validate() to manually trigger validation. ... Start by assigning a ref to the form so we can later validate...
Read more >
Validation - Formik
<Formik> and withFormik() take a prop/option called validate that accepts either a synchronous or asynchronous function. Copy. 1 // Synchronous validation. 2 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found