Formik 和 yup 验证正在验证一个非必需的文件输入字段
Formik and yup validation is validating a non-required file input field
我有一个输入字段(类型文件),尽管它不是必需的,但出于某种原因验证了它。
有了序列号,我有一个验证,它不是必需的,似乎工作正常,提交空字段时没有错误。对于文件输入字段,我只希望输入字段在上传文件时进行验证。我做错了什么?,当我点击提交测试验证时,我得到了这个(见下图)。
这是我的代码:
const validationSchema = Yup.object({
title: Yup.string()
.required("Title is required")
.min(8, "Title must be at least 8 characters")
.max(100, "Title cannot be more than 100 characters"),
description: Yup.string()
.required("Description is required")
.min(8, "Description must be at least 8 characters")
.max(100, "Description cannot be more than 100 characters"),
serialNumber: Yup.string()
.min(4, "Serial number must be at least 4 characters")
.max(16, "Serial number cannot be more than 16 characters"),
productStatus: Yup.number().required("A Status is required"),
productType: Yup.number().required("A type is required"),
img: Yup.mixed()
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => file && file.size <= 1100000
)
.test(
"fileType",
"Incorrect file type",
(file) =>
file && ["image/png", "image/jpg", "image/jpeg"].includes(file.type)
),
});
{formik.errors["img"] && formik.touched["img"] && (
<div style={{ color: "#B2484D", fontSize: ".8rem" }}>
{formik.errors.img}
</div>
)}
<Field name="img">
{() => {
// < - this will take fieldProps.. not used in this case, using formik instead
return (
<>
<input
onBlur={formik.handleBlur}
onChange={({ currentTarget }) => {
const file = currentTarget.files![0];
const reader = new FileReader();
if (file) {
reader.onloadend = () => {
setSelectedFile({
file,
previewURI: reader.result!,
});
setuploadbtnLabel(
`You selected: ${file.name} - click again to change`
);
};
reader.readAsDataURL(file);
formik.setFieldValue("img", file);
}
}}
ref={inputFile}
type="file"
style={{ display: "none" }}
accept=".png,.jpg,.jpeg"
/>
</>
);
}}
</Field>
编辑:这是一个代码框https://codesandbox.io/s/thirsty-chandrasekhar-34q1q?file=/src/App.js
我认为您应该尝试将 notRequired()
甚至 nullable()
添加到 YUP 模式定义的 img
字段中。来自 Yup docs:
Mark the schema as not required. Passing undefined (or null for nullable schema) as value will not fail validation.
编辑: 根据您提供的沙箱,我已经弄清楚了。问题来自您添加的测试。基本上问题是 file
是 undefined 当没有选择文件时,验证总是失败。
例如,您可以更改为:
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => file && file.size <= 1100000
)
为此:
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => {
if (file) {
return file.size <= 1100000;
} else {
return true;
}
}
)
notRequired()
或 nullable()
甚至不需要让它按照您期望的方式工作。这是此修复后的 link 到 sandbox。如果这对您有用,请将答案标记为已接受。
祝你好运!
我有一个输入字段(类型文件),尽管它不是必需的,但出于某种原因验证了它。 有了序列号,我有一个验证,它不是必需的,似乎工作正常,提交空字段时没有错误。对于文件输入字段,我只希望输入字段在上传文件时进行验证。我做错了什么?,当我点击提交测试验证时,我得到了这个(见下图)。
这是我的代码:
const validationSchema = Yup.object({
title: Yup.string()
.required("Title is required")
.min(8, "Title must be at least 8 characters")
.max(100, "Title cannot be more than 100 characters"),
description: Yup.string()
.required("Description is required")
.min(8, "Description must be at least 8 characters")
.max(100, "Description cannot be more than 100 characters"),
serialNumber: Yup.string()
.min(4, "Serial number must be at least 4 characters")
.max(16, "Serial number cannot be more than 16 characters"),
productStatus: Yup.number().required("A Status is required"),
productType: Yup.number().required("A type is required"),
img: Yup.mixed()
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => file && file.size <= 1100000
)
.test(
"fileType",
"Incorrect file type",
(file) =>
file && ["image/png", "image/jpg", "image/jpeg"].includes(file.type)
),
});
{formik.errors["img"] && formik.touched["img"] && (
<div style={{ color: "#B2484D", fontSize: ".8rem" }}>
{formik.errors.img}
</div>
)}
<Field name="img">
{() => {
// < - this will take fieldProps.. not used in this case, using formik instead
return (
<>
<input
onBlur={formik.handleBlur}
onChange={({ currentTarget }) => {
const file = currentTarget.files![0];
const reader = new FileReader();
if (file) {
reader.onloadend = () => {
setSelectedFile({
file,
previewURI: reader.result!,
});
setuploadbtnLabel(
`You selected: ${file.name} - click again to change`
);
};
reader.readAsDataURL(file);
formik.setFieldValue("img", file);
}
}}
ref={inputFile}
type="file"
style={{ display: "none" }}
accept=".png,.jpg,.jpeg"
/>
</>
);
}}
</Field>
编辑:这是一个代码框https://codesandbox.io/s/thirsty-chandrasekhar-34q1q?file=/src/App.js
我认为您应该尝试将 notRequired()
甚至 nullable()
添加到 YUP 模式定义的 img
字段中。来自 Yup docs:
Mark the schema as not required. Passing undefined (or null for nullable schema) as value will not fail validation.
编辑: 根据您提供的沙箱,我已经弄清楚了。问题来自您添加的测试。基本上问题是 file
是 undefined 当没有选择文件时,验证总是失败。
例如,您可以更改为:
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => file && file.size <= 1100000
)
为此:
.test(
"fileSize",
"File size too large, max file size is 1 Mb",
(file) => {
if (file) {
return file.size <= 1100000;
} else {
return true;
}
}
)
notRequired()
或 nullable()
甚至不需要让它按照您期望的方式工作。这是此修复后的 link 到 sandbox。如果这对您有用,请将答案标记为已接受。
祝你好运!