How can I trigger validation with a ref?
See original GitHub issueDescription
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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@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
@bluebill1049 Sure thing. what does CSB stand for?