Open burtek opened 5 years ago
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.
@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 usinginitialvalues
. If theonChange
is triggered, then theisValid
will be correct.
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 formik@1.4.3
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.
I also needed the initial error object to be correct. I wrote this little helper for sync cases:
import { useMemo } from 'react'
import { FormikErrors, yupToFormErrors } from 'formik'
import { ObjectSchema } from 'yup'
export default function useInitialErrorsSync(
schema: ObjectSchema,
initialValues: any,
): [boolean, FormikErrors<any>] {
const values: [boolean, FormikErrors<any>] = useMemo(() => {
try {
schema.validateSync(initialValues, { abortEarly: false })
} catch (err) {
if (err.name === 'ValidationError') return [false, yupToFormErrors(err)]
}
return [true, {}]
}, [])
return values
}
can be used like
...
const [isInitialValid, initialErrors] = useInitialErrorsSync(schema, initialValues)
return <Formik
validateOnChange
validateOnBlur
validateOnMount
validationSchema={schema}
isInitialValid={isInitialValid}
initialErrors={initialErrors}
initialValues={initialValues}
onSubmit={onSubmit}
>
...
isInitialValid is deprecated and will be removed in formik soon, it is advised to use validateOnMount
Hello guys, I am using
formik@1.4.3
and the following code works perfect:<Formik initialValues={initialValues} validationSchema={schema} isInitialValid={() => schema.isValidSync(initialValues)} onSubmit={onSubmit} enableReinitialize > ...
Please try with your projects.
In my case enableReinitialize was the game changer.
🐛 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 setsisValid
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 setisValid
to trueReproducible example
https://codesandbox.io/s/py95r24n7x
Typing anything in the field changes form state, but keeps
isValid
set tofalse
If you change validate to
const validate = values => ({});
,isValid
will be set totrue
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