Checkbox input errors seems to 'lag' behind current values
See original GitHub issueBug, 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:
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:
- Created 6 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
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 notvalue
, and the library needs to check forevent.target.checked
rather thanevent.target.value
.I’ve spent some more time digging into this and the underlying issue seems to be that checkboxes don’t emit
blur
orfocus
events, see this example Codepen. I’m not sure what the best workaround is, perhaps we could special-case the handling ofchange
events for checkbox inputs to normalise their behaviour?