How can I manually trigger form validation in React Hook Form, I would like to achieve effect of handleSubmit, but in keypress event handler and handleSubmit does not seem to work outside onPress. In the onPress handler, my code looks like so
<Button
color={enter ? COLORS.greys["gray-100"] : COLORS.TERTIARY}
textColor={COLORS.FONT_COLOR}
onPress={handleSubmit((data) => onSubmit(data))}
>
Enter
</Button>
and when I click this button and fields are invalid the error message shows up, I want to do this same if user clicks Enter, but this code does not work
const changeView = (e: KeyboardEvent) => {
if (e.key === "Enter") {
handleSubmit((data) => onSubmit(data));
}
};
useEffect(() => {
document.body.addEventListener("keydown", changeView);
return () => {
document.body.removeEventListener("keydown", changeView);
};
}, []);
I use getValues hook with onSubmit to make it work, but if the fields are invalid the form is not being validated
const changeView = (e: KeyboardEvent) => {
if (e.key === "Enter") {
onSubmit(getValues());
}
};
How can I validate the form in the event listener's handler?
you can use fromik to form library and yep library to validation this is usefull