React hook form - Register field not working

Viewed 6060

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.

3 Answers

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 } )}

I encountered the same issue. And I found that it happened because the version I installed is v6 but the official doc is v7 and the usage of register is different between two versions.

Related