I'm having some issues for a couple days now where every error that I set in the onSubmit method of my Formik form are not added to the errors props. My specific issue is concerning the backend errors that are returned by the GraphQL API. I have a pretty long form so I won't post all of my code but the relevant code is as follows :
This is a snippet of my form :
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
>
{({ errors, setFieldValue, touched, values }) => (
<>
{console.log(errors)}
<Form>
<div className="divided-by-2">
<div className="street-number-and-street">
<FormikTextField
id="streetNumber"
name="streetNumber"
label={i18next.t('model:traveler.streetNumber')}
/>
<FormikTextField
id="street"
name="street"
label={i18next.t('model:traveler.street')}
/>
</div>
<FormikTextField
id="city"
name="city"
label={i18next.t('model:traveler.city')}
/>
<FormikTextField
id="zip"
name="zip"
label={i18next.t('model:traveler.postCode')}
/>
</div>
</Form>
</>
)}
</Formik>
The onSubmit is a function that I declared outside of the Formik declaration because it is quite extensive. It goes as follows :
const onSubmit = async (values, actions) => {
const variables = {
firstname: values.firstname,
lastname: values.lastname,
[The rest of my variables are also here including the ones in the above snippet]
}
const { data } = await profileUpdate({
variables,
});
if (data.userUpdate.errors.length === 0) {
const { data } = await usersRoleRequestCreate({
variables: {
role: 'explorer',
note: values.note,
token,
},
});
if (data?.usersRoleRequestCreate?.errors.length > 0) {
for (const error of data?.usersRoleRequestCreate?.errors) {
console.log(error.attribute);
const key = Mapping[error.attribute];
if (key) {
let message = '';
for (const errorMessage of error.errors) {
message += errorMessage.message;
}
actions.setFieldError(key, message)
}
}
} else {
[If no errors, success code here]
}
}
}
I have tried to use setFieldError as shown here but I also tried using setErrors, creating an object using useState to store my backend validation errors and then assigning those values to errors, all of which were unsuccessful. The only event in which errors has a value is when I receive frontend errors from the yup validationschema. The Formik documentation doesn't provide a lot of examples (https://formik.org/docs/api/formik) and I really can't figure it out.
Thank you for your help