Validation on Blur Fires the Form's onSubmit - Is that expected? #870
Answered
by
chimame
kevinhpage
asked this question in
Q&A
-
I am using In this example, tabbing between the 'input' fields will trigger 'onSubmit'. I wouldn't expect 'onSubmit' to fire unless the submit button is pressed: const Test = () => {
const submit = useSubmit();
const lastResult = useActionData<typeof action>();
const [form, fields] = useForm({
lastResult,
shouldValidate: 'onBlur',
onValidate({ formData }) {
return parseWithZod(formData, { schema: schema });
},
});
// This is firing on every 'blur'. Disabling "shouldValidate: 'onBlur'" makes it stop.
const onSubmit = (e: FormEvent) => {
console.log('onSubmit', e);
e.preventDefault();
// submit(e.currentTarget as HTMLFormElement); // <-- Submits the whole form on every blur
};
return (
<Form id={form.id} method="post" onSubmit={onSubmit}>
<TextField label='Field 1' name={fields.field1.name} />
<TextField label='Field 2' name={fields.field2.name} />
<Button type="submit">Submit</Button>
</Form>
);
}; Thanks! |
Beta Was this translation helpful? Give feedback.
Answered by
chimame
Feb 23, 2025
Replies: 1 comment 1 reply
-
Do you want to handle const Test => {
const [form, fields] = useForm({
lastResult,
shouldValidate: 'onBlur',
onValidate({ formData }) {
return parseWithZod(formData, { schema: schema });
},
onSubmit(e) {
console.log('onSubmit', e);
}
});
return (
<Form id={form.id} method="post" onSubmit={form.onSubmit}>
<TextField label='Field 1' name={fields.field1.name} />
<TextField label='Field 2' name={fields.field2.name} />
<Button type="submit">Submit</Button>
</Form>
);
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kevinhpage
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to handle
onSubmit
after validating the data? If so, is this what you want to do?https://conform.guide/api/react/useForm#options