formik validationSchema not working with mui DatePicker

Viewed 24

I want to add validation for mui datepicker. in validationSchema without required method it's working fine But I want this datepicker to be required field

here's my code

const formik = useFormik({
    initialValues: {
        ...
    },

    validationSchema: yup.object({
        market: yup
            .object()
            .required("Fill out this field")
            .nullable(),
        dateFrom: yup
            .date()
            .required("Start date is required")
            .nullable(),
        dateTo: yup
            .date()
            .required("End date is required")
            .nullable(),
    }),
    onSubmit: async (values, helpers) => {
        ...
    }
})

<DatePicker
onChange={value => formik.setFieldValue("date_from", value, true)}
inputFormat="dd/MM/yyyy"
label="Trading Date"
value={formik.values.date_from}
renderInput={(inputProps) => (
    <TextField
    error={Boolean(formik.touched.date_from && formik.errors.date_from)}
    helperText={formik.touched.date_from && formik.errors.date_from}
    name="date_from"
    {...inputProps}
    />
)}/>
0 Answers
Related