question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom validate makes `formState.isValid` return false when no errors exist

See original GitHub issue

Describe the bug Changing validate object using setState causes formState.isValid to remain false even when all fields are valid.

We have a custom input component used throughout our app. It has a prop which specifies “Standard validations” e.g. ID regex, existing IDs etc. It also accepts custom validations. Inside the input component, we combine these two using setState, this seems to be causing this issue.

To Reproduce Steps to reproduce the behaviour:

  1. Go to sandbox
  2. Input something into the first(required) field
  3. You will see that formState.isValid is still false

Codesandbox link (Required) https://codesandbox.io/s/validate-error-on-setstate-puq3g?file=/src/App.tsx

Expected behaviour When the first(required) field is filled, formState.isValid should be true. The second field should only be invalid if a value is entered AND it doesn’t match the ID regex

Desktop (please complete the following information):

  • OS: macOS v11.1
  • Browser: Chrome
  • Version: 90.0.4430.93

Additional context I noticed this when migrating from 6.12.2 => 7.4.0. The issue didn’t occur before migration. After playing around with the versions on CSB, this issue occurs when moving from 7.1.1 => 7.2.0. Here’s the change for that version

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bluebill1049commented, May 7, 2021

no worries @damien-duignan the easiest fix should be just to keep your validation consistent from the initial render, so I would suggest writing your validation logic inside validate instead of useState with external dependency, at least hook form aware it will always have validation rather than first render null and then assigned validation subsequences called.

0reactions
damien-duignancommented, May 7, 2021

Thanks, I’ve resolved the issue now

Read more comments on GitHub >

github_iconTop Results From Across the Web

isValid is sometimes false despite no errors #2755 - GitHub
This issue is related to disabled , I think it makes sense to read the value during validation but not at submit.
Read more >
'isValid' is always false - Stack Overflow
However, regarding my main question, I tried adding the name to input, and it does not work. I would find it strange if...
Read more >
useFormState - Simple React forms validation
Set to true if the form doesn't have any errors. setError has no effect on isValid formState, isValid will always derived via the...
Read more >
React Hook Form: A guide with examples - LogRocket Blog
Learn all about using forms in React with React Hook Form, including how to create and validate forms — even with third-party components....
Read more >
useForm - VeeValidate
useForm is a custom composition API function that allows you to group fields ... any; // A yup schema, or a Record<string, any>...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found