New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to validate formik & Yup validation with file type and size ? #926
Comments
Yup doesn’t handle this. You can use your own validation function at either the form or field-level. |
After following this tutorial to integrate a file upload component in my form where FILE_SIZE is a numeric constant and SUPPORTED_FORMATS is an array of valid image type strings ['image/jpg', 'image/jpeg', 'image/gif', 'image/png'] |
Try this: |
@alecchisi When I do yup.object().shape({
file: yup.mixed().required('A file is required')
.test('fileFormat', 'PDF only', (value) => {
console.log(value); return value && ['application/pdf'].includes(value.type);
}), In my console only appears <input
id="file"
name="file"
type="file"
onChange={handleChange}
className={
errors.password && touched.password
? 'form-control is-invalid'
: 'form-control'
}
/> What could be possibly wrong? |
That is correct, because the html5 |
@alecchisi Ahh, cool. Thanks! |
@alecchisi What if I want file to be an optional and also if any file selected validate file size and file type? |
I refactored alecchisi's example into something more simple and functional https://codesandbox.io/s/keen-morning-otxz6 |
@ZephSibley While updating the Formik and Yup libraries in your code sandbox it just stopped working. Can you please look at it? Because I wasted a lot of time on this. So, if you update this it'll help people who are looking for the same kind of solution. |
Hi, I'm running into the same issue as @mariselvanm, I believe. I've isolated it in a sandbox. With version 1.5.8 of Formik, I'm able to see the file size in the console when I select a picture: https://codesandbox.io/s/yup-formik158-86xny In this sandbox, I've just updated the version of Formik to 2.0.3, and I've got |
@jaredpalmer Is there any way we can validate using |
@mariselvanm yes you can use both. I would not suggest it though. The results will be deeply merged. |
@alecchisi I follow your example but variable PS:
|
Hi there, Sorry to comment on a closed issue but I think this may be useful to some people: The solution posted by @alecchisi works very well for file uploads IF the field is required, however this will not work if the field is not required - for example if a file upload is optional for the user. In this case, the solution will fail because the default value for the In order to fix this for non required file uploads, I have the following yup scheme: const validationSchema = object().shape({
file: mixed()
.test('fileSize', 'File too large', (value) => value === null || (value && value.size <= FILE_SIZE))
.test(
'fileFormat',
'Unsupported file type',
(value) => value === null || (value && SUPPORTED_FORMATS.includes(value.type))
)
})
const formik = useFormik({
initialValues: {
file: null
// More values here
},
// More code here Just to also note, substituting this for |
@alex-r89 what if i want to check the height and the width of the image? Do you know how can I do this? |
what about with multiple files? |
Use validation in a separate file: const { errors } = props function isEmpty(obj) { const isValid = isEmpty(errors) |
This is 2021, so I have assumptions something changed about that as the current implementation. The code below when I console log the value object it returns VALUE C:\fakepath\ADVERT-PROCUREMENT-MANAGER-GMB.pdf If I console log
What I want to achieve is simple.
|
I was facing the same problem. Thank you! |
For anyone received
Note: Apart from |
Replace line with following: |
The subject must be even close but the solution which i found was that simple
`
Hope it will help someone |
Hi
I couldn't find any doc for file upload validation in formik and yup. Please help me how to do that ?
The text was updated successfully, but these errors were encountered: