According to validation reference FAQ returning undefined as error message is considered as marking field valid. Yet returning { fieldName: undefined } from validation function sets isValid tofalse thus preventing form from submitting. This is caused by following line in code:
Returning { fieldName: undefined } from validation function should set isValid to true
https://codesandbox.io/s/py95r24n7x
Typing anything in the field changes form state, but keeps isValid set to false
If you change validate to const validate = values => ({});, isValid will be set to true
At least change the docs.
To make the validation work according to current docs, you'd need to check all values (incl. nested values) in object returned by validation function.
| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.3.2 |
The documentation is pretty weird around that line. This is the code that decides whether you have errors.
Object.keys(this.state.errors).length - this snippet pretty much means that any set field (even with undefined) will result in isValid being false.
I recommend clearing your own object of any undefined properties before returning the validation errors to formik. This could work before returning -
Object.keys(errors)
.filter(key => errors[key] !== undefined)
.reduce((res, key) => {
res[key] = errors[key];
return res;
}, {})
An issue relating to isValid, not sure if I should start a new issue.
I noticed that the isValid field stays false after submitting a form if the values are set using initialvalues. If the onChange is triggered, then the isValid will be correct.
https://codesandbox.io/s/302vp2wqxq

@webberwang I ran into the same issue, but then found https://jaredpalmer.com/formik/docs/api/formik#isinitialvalid-boolean
@webberwang This seems be to work for me.
Using @barrett-vegas-com response with isInitialValid,
const validate = (values: Values): FormikErrors<Values> => { ... };
const isInitialValid = (values: FormikConfig<Values> & IProps) =>
Object.keys(validate(values.initialValues)).length === 0;
withFormik({ ...
isInitialValid,
validate
})
In case you are using yup
const isInitialValid = schema.isValidSync(initialValues);
This has been addressed is #1410
An issue relating to
isValid, not sure if I should start a new issue.I noticed that the
isValidfield stays false after submitting a form if the values are set usinginitialvalues. If theonChangeis triggered, then theisValidwill be correct.https://codesandbox.io/s/302vp2wqxq
put this inside
isInitialValid={true}
its working ..
on Edit Form initial values always true....
https://jaredpalmer.com/formik/docs/api/formik#isinitialvalid-boolean
Is there a way to set the is valid manually? When I set the field value from session storage and pass the same value to the initial values, the invalid is being set to true upon error though I trigger setFieldError.
@vinodh99 Try this:
const initialValues = {
... initial values
};
<Formik
.... other props
initialValues={initialValues}
validationSchema={schema}
isInitialValid={schema.isValidSync(initialValues)}
>
... rest of code
</ Formik>
This issue has been fixed in 2.x only, 1.x versions has to use isInitialValid.
I'm still having this issue. I'm using version 2.1.5 and even without any validation isValid is always false
I'm still having this issue. I'm using version 2.1.5 and even without any validation isValid is always false
Have you managed to find a way around or a fix for this?
Hello guys, I am using [email protected] and the following code works perfect:
<Formik
initialValues={initialValues}
validationSchema={schema}
isInitialValid={() => schema.isValidSync(initialValues)}
onSubmit={onSubmit}
enableReinitialize
>
...
Please try with your projects.
isInitialValid={() => schema.isValidSync(initialValues)}
That works, thanks!
But when I do
isInitialValid={schema.isValidSync(initialValues)}
it fails.
Most helpful comment
In case you are using
yup