Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formState.isValid return true in initial render with mode onChange,onBlur #1512

Closed
polovi opened this issue Apr 27, 2020 · 11 comments
Closed
Labels
question Further information is requested

Comments

@polovi
Copy link

polovi commented Apr 27, 2020

Is your feature request related to a problem? Please describe.
With onSubmit mode isValid initially return false but with onBlur, onChange it is true before validation is executed

https://codesandbox.io/s/flamboyant-lake-limo5?file=/src/App.js

Describe the solution you'd like
The form should be invalid until validation is executed

@bluebill1049
Copy link
Member

This is expected behavior, react hook form is embraced uncontrolled input. the validation can only be assured after render.

@bluebill1049 bluebill1049 added the question Further information is requested label Apr 27, 2020
@polovi
Copy link
Author

polovi commented Apr 27, 2020

I understand when validation is executed but with onBlur and onChange mode he start with state isValid=true before he really validate inputs and then is changed to false. So with initial render I must add another condition to check false positive state. With initial false value it will be true only when validation is passed

@bluebill1049
Copy link
Member

I understand when validation is executed but with onBlur and onChange mode he start with state isValid=true before he really validate inputs and then is changed to false. So with initial render I must add another condition to check false positive state. With initial false value it will be true only when validation is passed

why would you need to do that? it's the second render, this is not going to impact user experience right?

@polovi
Copy link
Author

polovi commented Apr 27, 2020

User experience... no. But if I have some conditional rendering like {formState.isValid && <CustComponent values={allWatched}/>} e.g. its rendered in state that in fact is false positive(updated codesandbox). I think it should start as invalid form when validation didn't passed when logically he could validate only after all inputs are rendered.

@bluebill1049
Copy link
Member

yes @polovi that's a challenge to solve, if you rewind back to v3 we had false to start with.

if we start as false,

then as we render all inputs

input 1. pass -> render true
input 2. false -> render false

you see multiple re-render will have to decide the form is vald or not, that's the reason why we start with true and re-render only once.

@polovi
Copy link
Author

polovi commented May 1, 2020

And what if will be possible to enforce initial validation with some useForm() option. So when we know that form is really invalid on start it could skip validation like with mode onSubmit.

@bluebill1049
Copy link
Member

bluebill1049 commented May 1, 2020

it's already optimized to skip with onSubmit.

@cmnstmntmn
Copy link

i'm having the same needs for the isValid to be false at start, but i found out that i can use dirty bool as an extra condition to achieve what i needed.

@jacdx
Copy link

jacdx commented Sep 23, 2020

Hi @bluebill1049, love the library. I was bit by this issue pretty hard today. I'm using RHF in React Native with a form that doesn't have a submit button. So I use watch + a useEffect hook to watch formState.isValid. Based on isValid, I determine when updates should be made to the model. But since isValid gives a false positive, I have to put in a sketchy check. I need to always trust isValid. I understand why you can't know on initial render, but I need to be able to account for this part of the lifecycle. Perhaps a formState.isInitializing or something that would let me know what RHF is doing during this phase. Using @latest and onChange BTW.

@bluebill1049
Copy link
Member

Hi @bluebill1049, love the library. I was bit by this issue pretty hard today. I'm using RHF in React Native with a form that doesn't have a submit button. So I use watch + a useEffect hook to watch formState.isValid. Based on isValid, I determine when updates should be made to the model. But since isValid gives a false positive, I have to put in a sketchy check. I need to always trust isValid. I understand why you can't know on initial render, but I need to be able to account for this part of the lifecycle. Perhaps a formState.isInitializing or something that would let me know what RHF is doing during this phase. Using @latest and onChange BTW.

how about keeping a single useRef at your form level?

const isInital = useRef(true)
useEffect(() => isInital.current = false, [])
// isInital.current use this

@jacdx
Copy link

jacdx commented Sep 26, 2020 via email

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants