Formik: Checkbox doesn't bind to initialValues

Created on 26 Oct 2018  路  18Comments  路  Source: formium/formik

馃悰 Bug report

Current Behavior

When using <Field type="checkbox /> and initialValues the checkbox doesn't bind.

Expected behavior

<Field type="checkbox" /> should bind to initialValues

Reproducible example

https://codesandbox.io/s/ppy7wyrl0m

Suggested solution(s)

See #1024

Additional context

See #1024

Your environment

| Software | Version(s) |
| ---------------- | ---------- |
| Formik | 1.3.1
| React | 16.5.2

Field Enhancement Feature Request stale

Most helpful comment

This is still an issue

All 18 comments

This worked as a workaround for me.

const CheckboxInput = (props) => (
    <Field {...props} render={({field}) => <input {...field} type="checkbox" checked={field.value} />} />
);

This workaround is a little simpler, adding the checked property on the Field (works since the props get spread onto the input)

<Field type="checkbox" name="box" checked={props.values.box} />

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

I think this is still a problem.

In the following sandbox, the initialValues only works when <Field /> isn't given type="checkbox".

https://codesandbox.io/s/p7zlvk86lm

I also think this is still a problem - can't seem to set checkbox initial values using initialValues

Seems that this is being fixed here #1115

This is still an issue

This is pretty sad guys...

Updated the initial codesandbox to Formik 2 and React 16.8.6 (something with hooks)

https://codesandbox.io/s/formik-v2-field-checkbox-binding-vdvty

Please suggest work around for checkbox with Field render for formik 1.5.7

Please suggest work around for checkbox with Field render for formik 1.5.7

@prajavk , It works for me:

<Field type="checkbox" name="manual-only" id="manual-only" onChange={event => setFieldValue('manualOnly', event.target.checked)} checked={values.manualOnly} />

Workaround with react-native

<CheckBox color="#000"
                      onPress={event =>
                        props.setFieldValue("terms", !props.values.terms)
                      }
                      checked={props.values.terms}
                    />

Still experience this in formik 1.5.8
Hoping more comments results in this getting patched.

Followed @RazerM 's comment and removed type="checkbox" as a workaround.

I have the same problem. Third formik bug I have run into since starting to use the library last week... faith has been shaken.

Not fixed yet

Yep, lost an hour or so before realising this is a straight up bug.
Please re-open the issue.

The checked={value} type workaround is usable but it's unexpected that initialValues doesn't work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

green-pickle picture green-pickle  路  3Comments

emartini picture emartini  路  3Comments

sibelius picture sibelius  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments

Jungwoo-An picture Jungwoo-An  路  3Comments