react-hook-form: isValid is sometimes false despite no errors
Describe the bug
I sometimes get isValid = false despite having no visible errors and errors object being an empty object ({})
To Reproduce I do not yet have a reproducable sandbox, but I already found a code piece that might be the problem. Please see additional context
Expected behavior
isValid should always reflect what the errors object contains (e.g. isValid = Object.keys(errors).length === 0)
Desktop (please complete the following information):
- OS: Windows 10 202001
- Browser: Microsoft Edge
- Version: 86.0.621.0 (Official build) canary (64-bit)
Additional context
The problematic line could be this code piece here: https://github.com/react-hook-form/react-hook-form/blob/03216edb0b29bae631fb752e6ec612111d82798d/src/useForm.ts#L341 which triggers revalidation but without providing an isValid parameter as the 4th parameter. Hence this line https://github.com/react-hook-form/react-hook-form/blob/03216edb0b29bae631fb752e6ec612111d82798d/src/useForm.ts#L218 will resolve in !!isValid which essentially means !!undefined === false.
AFAIK the problem only appears on async filled inputs (inputs that get their value via an api request).
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 61 (41 by maintainers)
Commits related to this issue
- fix #2755 with disabled input — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
- š fix issues #2785 #2755 (#2789) * fix #2785 issue with async nested field array reset * fix #2755 with disabled input * improve getFieldValue API * fix fieldArray dep and fix variable name... — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
- close #2755 with radio input same name — committed to react-hook-form/react-hook-form by bluebill1049 4 years ago
https://react-hook-form.com/api/useform/formstate
Why is this closed when the issue still exists?
I am running into this issue too. It works with
mode: 'all', reValidateMode: 'onChange', but we donāt want to trigger validations before submitting the form first.Destructuring
formStateas described above also doesnāt seem to work.So the fix for me was reading
form.formState.errorsbefore performing thetrigger(), even though the errors are always empty. Reading the errors made thetrigger()execute correctly and updated theform.formValuesproperties.hack until this is fixed.
we can assert form validity by not having errors.
isEmpty(methods.formState.errors)for initial validation failure we can trigger validations by
Running into this issue after updating packages on an existing app that was working as expected. Tried suggestions above with no success.
Original versions before updating (works): RHF v7.4.1 @hookform/resolvers v2.4.0 yup v0.32.9
Currently on versions (issue): RHF v7.11.0 @hookform/resolvers v2.6.1 yup v0.32.9
The form starts in a locked/disabled state (not sure if relevant). User clicks button to unlock the form. There is a Material-UI Switch component for a boolean field. It does not matter if it start as
trueorfalse. The first change of the Switch always setsisValid = falsewith an emptyerrorsproperty.Any following toggles of the Switch give the expected
isValid = trueresult. The entire form always has default values. Iāve been inspecting the form values while debugging and those look fine.The Yup validator only requires that the fields are defined - which they are in all cases as far as I can tell.
[Edit] Also worth mentioning, I have a chunk of code in the component that allows me to toggle some console logs. If I set
debug = true, then this works as expected. Not sure why that is at this point.@stefanpl This has been improved: https://github.com/react-hook-form/react-hook-form/releases/tag/v7.39.0
Just another data point for analysis, I have this error when using autocomplete from my browser in an input field. When I type, no issues. When I just select the suggested option, no errors and
!isValid.Having similar issue now was there any solution
for me it is not that the functionality does not work, it is the tests that are failing when using āisValidā in the check.
@tar-aldev which version are you using? The solution doesnāt work for me. (v7.9.0)
@bluebill1049 Sorry to spam a closed ticket, but since it has some activity. RFC: Howās the new
useFieldArrayrules going to play with this errors behavior and refs, currently usingformStateerrors with{ mode: 'onChange'}is far from usable, isValid is going to be true even with errors being shown on the UI, for more than a few use cases, using a trigger isnāt logical, basically means to re-execute the rules (that in my case are async ones).Unfortunately I didnāt recognize exactly when it started to happen⦠When Iām back on my PC I can ātime travelā and investigate
Hereās a simple variant with only 2 radios where
isValidis initiallyfalse, but when changed it getstrue: https://codesandbox.io/s/rhf-isvalid-initially-false-bug-3uchtAnd hereās a variant where the fields are required. The
isValidfield will always befalse: https://codesandbox.io/s/rhf-isvalid-always-false-bug-forked-uvb30It seems the problem is with radios. I also use radios in my usecase where it breaks.
Putting
trigger()inside of useEffect works for me!No worriees, @GProst if you want to force the errors state to be updated/rendered, you can try to call
trigger()(atuseEffect), that would validate the whole form and flush down thoseerrorsstateFor me doing this solved the problem
Although I am not sure if it gives some porblems when used this way, as I am quite new to react hook form
Uff, wow š Sorry for creating such hard problems. Regardless of how complicated things are though, you manage to solve them in just 30 mins of time - everytime. Youāre awesome and I canāt thank you enough. The CSB works, Iād love to give it a spin in my realworld app š
Thank you š
This one is trigger and hard⦠https://codesandbox.io/s/rhf-isvalid-always-false-bug-forked-q1xsf?file=/src/src/useForm.ts
what happens because your radio input shares the same name, but it gets unmounted and remounted with the same name. so it was working before because we had a bug not remove field with validation correctly (rare case). I think this PR should fix this. also we doing deepEqual with validation fields with valid fields now, thatās why itās picking up this issue.
let me know if the above CSB fix the problem. š
kk, i will take a closer look at it.
by the way, please read this: https://twitter.com/bluebill1049/status/1300231640392716288 just be careful with disabled input for submission. we are following the HTML standard in this lib.
Thanks very much, @krnlde š I will take a look at it tmr night.
Here I got the exact problem I have. Itās a bit tricky to force the
isValid: falsestate, but when you just switch between the first and the second radio, it should appear after 1 second (the current timeout delay, to emulate network request). https://codesandbox.io/s/rhf-isvalid-always-false-bug-forked-xgt5u?file=/src/App.tsxboth codesandboxs are returning true after i attached
mode: 'onChange'šWill do! Thank you š
Regarding not having the mode set to onChange: Shouldnāt it be
trueinitially anyways though?Hey! I have that for a while now, but it got worse somewhere between 6.4 and 6.6.
Iām not sure right now which mode Iām using, but I assume onChange.
also, both example doesnāt have
mode: 'onChange', perhaps you are after one of them is checked withvalidate? I think required: true with each radio/checkbox is perhaps the issue here. https://codesandbox.io/s/checkbox-min-checked-5vocdAlright! Iāll try to do a reproducable codesandbox š
Latest,
6.6.0. But it is the same with the previous (6.5.3)I can go back to the point where it worked and give the version.