I have an input field (type file) that for some reason validates even though is not required. With serial number i have a validation and it is not required and seems to work fine, no errors when submitting the empty field.. For the file input field I want only the input field to validate when a file is being uploaded. What am i doing wrong?, i get this(see image below) when i click submit to test the validation.
here's my code:
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>
EDIT: here's a codesandbox https://codesandbox.io/s/thirsty-chandrasekhar-34q1q?file=/src/App.js