Formik: isValid is false although all fields are valid

Created on 18 Nov 2018  路  14Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

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:

https://github.com/jaredpalmer/formik/blob/85d15b302cd776eb615064a4451e1a7575b1439d/src/Formik.tsx#L425

Expected behavior

Returning { fieldName: undefined } from validation function should set isValid to true

Reproducible example

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

Suggested solution(s)

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.

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.3.2 |

stale

Most helpful comment

In case you are using yup

const isInitialValid = schema.isValidSync(initialValues);

All 14 comments

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

Example

@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 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

Example

put this inside

isInitialValid={true}

its working ..
on Edit Form initial values always true....

suggested Link

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jungwoo-An picture Jungwoo-An  路  3Comments

jordantrainor picture jordantrainor  路  3Comments

outaTiME picture outaTiME  路  3Comments

sibelius picture sibelius  路  3Comments

najisawas picture najisawas  路  3Comments