Formik + Yup don't validate input if untouched

∥☆過路亽.° 提交于 2019-12-11 16:11:48

问题


I am trying to use Formik + Yup to validate my form, but currently if I touch one field, and then try and submit, a field that has a minimum length will validate, and not allow me to submit.

Following my code sandbox (https://codesandbox.io/s/focused-bardeen-5fiuz), I want the user to be able to submit the form if they just change the firstName field, but without entering anything for the country field. I tried both the .notRequired() and .nullable(true) params, but did not work. Any idea how to achieve this?


回答1:


You can try use custom validate with test. A working example here https://codesandbox.io/s/laughing-dust-f9wet

country: yup
      .string()
      .test("isValidCode", "Country must be 2-letters (ISO codes).", value => {
        if (!value) {
          return true;
        }
        return value.toString().length === 2;
      })


来源:https://stackoverflow.com/questions/56844264/formik-yup-dont-validate-input-if-untouched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!