Formik: validateOnMount does not validate field on first mount

Created on 28 Jul 2020  路  9Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

at first mount field has 0 errors with defined "initialValue" and "validateOnMount" flag

Expected behavior

at the first mount, the validation field must have an error.

Reproducible example

https://codesandbox.io/s/formik-codesandbox-template-723xp

Additional context

at [email protected] all works fine.

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 2.1.5
| React | 16.11.0

Most helpful comment

@cduff

Yes, I can confirm that it is fixed in 2.1.6.

All 9 comments

Looks like this works in 2.1.4 as well. I believe my PR #2625 will fix this.

same issue on 2.1.5

Any update on this? I'd like to be able to update Formik to keep my dependencies up to date but this issue is stopping me.

In the last version the property isInitialValid is marked as deprecated, but this was only way to disable submit button on initial empty form. Current initialErrors with validation schema makes the form valid on start even if it invalid (empty).

for now I'm using like this:

import React, { useRef, useEffect } from 'react'
import {
  Formik as BaseFormik,
  Form as BaseForm,
  FormikProps,
  FormikConfig,
} from 'formik'

function Formik<Values>(props: FormikConfig<Values>) {
  const { children, ...rest } = props
  const formRef = useRef<FormikProps<Values> | null>(null)

  useEffect(() => {
    if (formRef.current) {
      formRef.current.validateForm()
    }
  }, [])

  return (
    <BaseFormik innerRef={formRef} {...rest}>
      <BaseForm>{children}</BaseForm>
    </BaseFormik>
  )
}

// Workaround for: https://github.com/formium/formik/issues/2652
export default Formik

So instead of import from formik I'm using this component

Appears to be fixed in 2.1.6?

@cduff

Yes, I can confirm that it is fixed in 2.1.6.

Yep. CONFIRMED TOO -- The bug is present in 2.1.5, but fixed in 2.1.6 -- I just upgraded and tried it.

Yeap, confirm. It's working in 2.1.6

Was this page helpful?
0 / 5 - 0 ratings

Related issues

outaTiME picture outaTiME  路  3Comments

sibelius picture sibelius  路  3Comments

green-pickle picture green-pickle  路  3Comments

pmonty picture pmonty  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments