jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.88k stars 2.79k forks source link

How to skip validation for the field not exists in form using Formik + Yup? #2075

Open harleenarora opened 4 years ago

harleenarora commented 4 years ago

I want to add validation if a particular field exists in the form, otherwise, there should not be any validation or skip validation for that field.

How can I achieve this?

spawluk commented 4 years ago

use object({}).test()

kaldebert commented 4 years ago

You can also use when. For example, add a required validation on foo if bar is present :

foo: mixed().when("bar", {
    is: value => !!value,
    then: mixed().required()
  }),
luismasg commented 3 years ago

so what i just did

<Formik onSubmit={actualSubmitFunction} . .

<button onClick={vales.valueExists? submitForm(): actualSubmitFunction(values)}>submit </button>