下面是我使用的React
表单验证代码formik
。默认情况下,当表单加载时,我想禁用提交按钮:
import { useFormik } from "formik";
import * as Yup from "yup";
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
email: ""
},
validationSchema: Yup.object({
firstName: Yup.string()
.max(15, "Must be 15 characters or less")
.min(3, "Must be at least 3 characters")
.required("Required"),
lastName: Yup.string()
.min(3, "Must be at least 3 characters")
.max(20, "Must be 20 characters or less")
.required("Required"),
email: Yup.string()
.email("Invalid email address")
.required("Required")
}),
onSubmit: values => {
handleSubmit(values);
}
});
我试图在我的按钮上使用它:
disabled={!formik.isValid}
但只有当我尝试提交表单时它才真正有效。所以,如果我将表单留空并点击提交,所有验证错误都会显示出来,然后按钮被禁用。但是,它应该从一开始就被禁用。我检查了文档,但没有看到任何明显的东西。