0

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.

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.