Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React hook form - Register field not working

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.

like image 996
Nnp Avatar asked Oct 30 '25 22:10

Nnp


2 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 } )}
like image 73
Bill Avatar answered Nov 02 '25 12:11

Bill


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!

like image 40
Sergej Klackovskis Avatar answered Nov 02 '25 13:11

Sergej Klackovskis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!