如果未触摸,Formik + Yup不会验证输入 [英] Formik + Yup don't validate input if untouched
问题描述
我正在尝试使用Formik + Yup来验证我的表单,但是当前如果我触摸一个字段,然后尝试提交,则具有最小长度的字段将被验证,并且不允许我提交.
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.
在我的代码沙箱之后( https://codesandbox.io/s/focused-bardeen- 5fiuz ),我希望用户只要更改firstName
字段即可提交表单,而无需为country
字段输入任何内容.我同时尝试了.notRequired()
和.nullable(true)
参数,但是没有用.知道如何实现吗?
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?
推荐答案
您可以尝试对test
使用自定义验证.此处的工作示例 https://codesandbox.io/s/laughing-dust-f9wet
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;
})
这篇关于如果未触摸,Formik + Yup不会验证输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!