Reputation: 2027
I have a form that contains multiple inputs, I don't want to do the validation for each field, but if all the fields are empty, it shows an error on the top the form. Is there a way I can do it with formik? I was looking at "validate?: (values: Values) => FormikErrors | Promise" from https://jaredpalmer.com/formik/docs/api/formik#validate-values-values-formikerrors-values-promise-any, but I don't know how to write the errors object and where to show the error since I don't want the error to show under a specific field.
Upvotes: 0
Views: 2696
Reputation: 792
Formik has an onSubmit function that you can check values and see if there is a filled input and show error based on that and prevent user to submit an empty form
and of course in Formik render section you have access to values property and you can show an error if values are empty. but I guess it doesn't have the best user experience
<Formik
onSubmit={(values) => {
//here you can read values object and set your custom error in state and then show to the users.
}}
>
{({ handleChange, errors, values, isSubmitting, handleSubmit }) => (
<Form>
// you have access values here too.
...
</Form>
Upvotes: 1