question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pickers make Formik consider the Form to be always invalid

See original GitHub issue

Including 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:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
CharlestonREMcommented, Feb 24, 2021

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.

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

0reactions
CharlestonREMcommented, Feb 24, 2021

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.

image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found