here is my codesandbox, I'm manually registering two fields, if I click submit w/o entering any data I see required field validation, which works fine and if I unregister "lastname" it removes error from last name, however,
The problem: first name still shows required validation even if I enter data. I tried removing manual registering a field and it work fine. But that is not my requirement. Any help. Thank you.
setValue doesn't trigger validation by default:
https://react-hook-form.com/api#setValue
- onChange={(e) => setValue("firstName", e.target.value)}
+ onChange={(e) => setValue("firstName", e.target.value, { shouldValidate: true } )}
Based on documentation you need to use "ref" in order to register needed field. Read here:
https://react-hook-form.com/api#register
I also updated your example:
https://codesandbox.io/s/react-hook-form-unregister-v6-forked-6wq09?file=/src/index.js
Have fun!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With