jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.88k stars 2.79k forks source link

Cannot change validationSchema and form values synchronously #2306

Open adamborowski opened 4 years ago

adamborowski commented 4 years ago

🐛 Bug report

Current Behavior

When you programatically setFieldValue and update validationSchema, formik still displays errors. It will remove errors once you update formik in any way, it validates form based on new values but old schema

Expected behavior

When you programatically setFieldValue and update validationSchema, formik will revalidate form based on new schema and new value

Reproducible example

Edit withFormik Codesandbox Template

The example shows initially validated form. Please click 'remove validation' which will remove value and validation at the same time. You should see no error but there are. You can check that validation is passed correctly by focusing and blurring the input field or just clicking validateForm() button

Suggested solution(s)

Formik should add useEffect to perform validation when validationSchema is changed. Validation should be done based on validationSchema from useRef, not props.

Additional context

Similar problems: https://github.com/jaredpalmer/formik/issues/1833 https://github.com/jaredpalmer/formik/issues/2266

Your environment

Software Version(s)
Formik 2.1.4
React 16.12.0
TypeScript Flow 0.113.0
Browser Chrome latest OSX
chopkins123 commented 4 years ago

Are there any workarounds for this? I'm having the exact issue on a form I'm building at the moment

juji commented 3 years ago

https://github.com/formium/formik/issues/1833#issuecomment-717086614