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.

Checkbox input errors seems to 'lag' behind current values

See original GitHub issue

Bug, Feature, or Question?

Let’s say I have a set of initial values that looks like this:

{ 
  email: '', 
  categories: { 
    foo: false
  } 
}

and I define a schema to validate that which looks like this:

Yup.object().shape({
  email: Yup.string()
    .email('Invalid email address')
    .required('Email is required!'),
  categories: Yup.object({
    'foo': Yup.boolean().is([true], 'Must be checked')
  })
})

When I go to submit that form, I should see errors that look something like this

"errors": {
  "email": "Email is required!",
  "categories": {
    "foo": "categories.foo must be one of the following values: true"
  }
}

If I then checked a checkbox input in my form with its name attribute set to categories.foo, I’d expect the errors to change to:

"errors": {
  "email": "Email is required!"
}

But right now that’s not the case.

Current Behavior

Currently the form’s errors don’t change, but when I uncheck the input, the error disappears. If I check the box again, the error reappears. However, if I check the checkbox, and then edit the email input on my form, both errors disappear. This is a little hard to explain, so I have a CodeSandbox example here:

Edit Formik Example

Desired Behavior

I should see the expected validation errors based on my input being checked or unchecked.

Suggested Solutions

Info

  • Formik Version: 0.11.0.beta.1
  • OS: macOS 10.13.2
  • Node Version: 9.3.0
  • Package Manager and version: npm 5.6.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Dremoracommented, Jan 23, 2018

I have a very similar issue. But in my case the problem is not with onBlur (which is triggered). It seems that on change yup (or a validation function) receives a value of "on" rather than a boolean.

Checkboxes and radios in React use checked property and not value, and the library needs to check for event.target.checked rather than event.target.value.

2reactions
rhysforyoucommented, Jan 22, 2018

I’ve spent some more time digging into this and the underlying issue seems to be that checkboxes don’t emit blur or focus events, see this example Codepen. I’m not sure what the best workaround is, perhaps we could special-case the handling of change events for checkbox inputs to normalise their behaviour?

Read more comments on GitHub >

github_iconTop Results From Across the Web

react hooks setState lagging one step behind - Stack Overflow
I have this checkbox input, when i click on it, i take its value and add to state array called vendorFilters but seems...
Read more >
All grid actions (checkbox, hover, scrolling, editing) are slowed ...
Problem is, when all the rows are rendered, the grid is slowed and lagged beyond use; hover states are not matching between fixed...
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked...
Read more >
How to Fix Input Lag - Intel
Controllers and keyboards can produce lag due to hardware errors, but it's not common. Switching input devices is an easy way to check...
Read more >
Leading vs Lagging Indicators: What's The Difference?
Leading and lagging indicators are qualifiers that assess a business's current state (lagging indicator) and predict future conditions ...
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