问题
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