subscribe a field to another field? #11716
Replies: 1 comment 1 reply
-
You could have a look at the I'm not sure if you would need to have the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to make react hook form retrigger the validation of a given field every time another field is updated? For example, imagine a password field and a confirm password. If the password is updated in such a way that they do not match anymore, I want to update the confirm password to show an error.
I know there is the
watch
api, and I solutioned the problem described above like this:const password = watch('password.password')
const isConfirmPasswordDirty = dirtyFields.password?.confirmPassword
useEffect(() => {
if (isConfirmPasswordDirty) {
trigger('password.confirmPassword')
}
}, [watch, password, trigger, isConfirmPasswordDirty])
However, this is kinda verbose, and it would be handy if there was a way to automatically subscribe one field to another by passing a prop to it. Is there such a thing in react-hook-form? And if not, is it possible to consider implementing such an api?
If the answer to both questions is no, I can always create a custom implementation in my code base. But I still wanted to post this here.
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions