0

I am using formik and Yup to handle by form data and Form Validation in react . I am trying to change the value of "Showfile" to true to do conditional validation But value is not changing.

                             

                                    
                                    TextField
                                    label="Resume"
                                    star="*"
                                    color="red"
                                    name="file"
                                    type="file"
                                    accept=".pdf ,.docx,.doc "
                                    onChange={(event) =>
                                        formik.setFieldValue(
                                            "file",
                                            event.currentTarget.files[0],
                                            "showfile",
                                            true
                                        )
                                    }
                                    value={formik.value}
                                />
                                
file: Yup.mixed().when("showfile", {
            is: true,
            then: Yup.mixed()
                .required("Required")
                .test(
                    "FILE_SIZE",
                    "Uploaded file is too big.",
                    (value) => value && value.size  value && SUPPORTED_FORMATS.includes(value.type)
                ),
Formik
            initialValues={{
                UserName: "",
                email: "",
                Phone: "",
                message: "",
                file: "",
                showfile: false,
                submit: false,
            }}
            validationSchema={validate}
            onSubmit={(values, actions) => {
                setTimeout(() => {
                    console.log(values);
                    actions.setSubmitting(false);
                }, 1000);
            }}
        >

1 Answer 1

0

You are using setFieldValue wrong way. It only updates for one field. If you want set 2 filed, you need to call setFieldValue 2 times:

onChange={(event) => {
    formik.setFieldValue("file", event.currentTarget.files[0]);
    formik.setFieldValue("showfile", true);
}}

You can read more about setFieldValue in here: https://formik.org/docs/api/formik#setfieldvalue-field-string-value-any-shouldvalidate-boolean--void

Sign up to request clarification or add additional context in comments.

4 Comments

ok.. that got resolved but now i am getting required error message just after selecting accepted file .... and on clicking other field it is disappearing.....
what should be the value of file field... here i have given value={formik.values.file}
I think you need to add another answer with the detail about your problem. After that, I can take a look and maybe can help you
Thank u for your help... that problem got resolved ... I am facing a new problem in validating my file field using Formik and Yup.... I have posted my question here...stackoverflow.com/questions/67323291/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.