jaredpalmer / formik

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

How to turn off validation? #2429

Open sandorvasas opened 4 years ago

sandorvasas commented 4 years ago

I've got field-level validation (validate prop) on a huge form, and have two submit buttons: Save as draft (no validation required), Submit (needs to execute validation). I found no easy way to do this. As I said it's a huge form and now it looks like I need to replace the way all fields are validated. It would be so much easier to just do something like

@withFormik({
  validationDisabled: true,
  handleSubmit: async (values, { props, validateForm }) => {
      if (props.requiresValidation) {
         await validateForm()
      }
  }
})

I know this is a duplicate, I found that many people brought this up, but being the library that advertises itself as 'without the tears'. Well, this lack of feature gives me tears.

johnrom commented 4 years ago

I think easiest way to do this would be to circumvent Formik altogether. After all, Formik is a form validation + submission library. However, your callback is not bound to Formik.

const getSubmitHandler = isDraft => 
  async (values) => ajax.post(isDraft ? 'your/api/save-draft' : 'your/api/submit', values);
const onSubmit = getSubmitHandler(false);
const onSaveDraft = getSubmitHandler(true);

const YourForm = () => <Formik onSubmit={onSubmit}>
    {({ values }) => <button onClick={() => onSaveDraft(values)} />}
</Formik>