Pickers make Formik consider the Form to be always invalid
See original GitHub issueIncluding the TimePicker, DatePicker or DateTimePicker in a form makes Formik consider the form as invalid. The errors property for the picker is set to an empty string and apparently that makes the form invalid.
Consider the following example:
import * as React from "react";
import { Formik, Form } from "formik";
import { TextField } from 'formik-material-ui'
import {
TimePicker,
DatePicker,
DateTimePicker
} from "formik-material-ui-pickers";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns";
export default function App() {
return (
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<Formik
initialValues={{
email: "",
dateTime: new Date()
}}
>
{({ errors, isValid }) => (
<Form>
<TextField name="field" label="Field" />
<br />
<TimePicker name="time" label="Time" />
<br />
<DatePicker name="date" label="Date" />
<br />
<DateTimePicker name="dateTime" label="Date Time" />
<br />
errors:
<pre>{JSON.stringify(errors, null, 2)}</pre>
isValid: {isValid ? "true" : "false"}
</Form>
)}
</Formik>
</MuiPickersUtilsProvider>
);
}
this will show errors to be:
{
"time": "",
"date": "",
"dateTime": ""
}
and the isValid
field to be false
.
https://codesandbox.io/s/sad-moore-i4b4k
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
Pickers make Formik consider the Form to be always invalid
Including the TimePicker, DatePicker or DateTimePicker in a form makes Formik consider the form as invalid. The errors property for the picker ......
Read more >React-datepicker with a Formik form - Stack Overflow
To support that, we just need to pass to this wrapper the isInvalid or error prop, and manually set className , e.g. className={"form-control...
Read more >Validation - Formik
Form -level validation is useful because you have complete access to all of your form's values and props whenever the function runs, so...
Read more >Forms with Formik & Yup - JavaScript in Plain English
Using Formik and Yup to wire forms up; Capturing customer data in ReactJS; Validating customer data and providing clean feedback instantly; ...
Read more >React Best Practices – Tips for Writing Better React Code in ...
I'm a huge believer that learning React doesn't make sense if you don't ... from "@material-ui/pickers"; import { Formik } from "formik"; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This was not the problem. I needed to set the value of the date picker and time picker fields to
null
in the initial values.https://github.com/stackworx/formik-material-ui/issues/147#issuecomment-785355948
time and date pickers default to current timestamp even if initialValues are set to empty strings
The initial values are set to empty strings, but somehow the fields supply the current time. The pickers are based on the codesandbox date picker and time picker.