Manual validation should have a flag to touch all fields

🚀 Feature request

Current Behavior

When submitting a form today, formik’s handleSubmit will run manual validation and then touch all fields to show the validation errors to the user.

If you run manual validation this behavior is not there, you have to manually check the results of the validation and touch all fields using setNestedObjectValues (basically mimic what handleSubmit does)

Desired Behavior

formik’s validate form should have a flag that lets the user have the exact same behavior as handleSubmit to avoid having to use setNestedObjectValues every time you trigger manual validation.

Suggested Solution

Adding an optional setTouched?: boolean parameter to validateForm to do this behavior.

I’ll be willing to do a PR for this! Let me know if you agree with the approach or if this has been considered but there’s a reason not to add it. Thanks!

1 possible answer(s) on “Manual validation should have a flag to touch all fields

  1. @CristianoYL I’ve found that there is, if you run formik.setTouched(setNestedObjectValues(errors, true)) where errors is the result of formik.validateForm() you get all the fields touched…. the problem is that you have to do it manually, instead of having an option to do it within validateForm… this is the code I have, which is basically a copy of what handleSubmit inside formik does.

    const errors = await form.validateForm();
    if (Object.keys(errors).length === 0) {
      // Form is valid, do any success call
    } else {
      form.setTouched(setNestedObjectValues<FormikTouched<FormValues>>(errors, true));
    }
    

    The problem is that if you want to touch all the fields every time you run manual validation you have to do this code all the time