Checkbox value breaking pristine state
See original GitHub issueHi, I found out that using true/false value within checkboxes is breaking the pristine state value. The case is: Checkbox initial value is probably an empty string. When I set a checkbox to checked state and then uncheck it, the pristine value changes. The flow is ‘’ -> true -> false. You can even check it in the simple example here: http://redux-form.com/6.6.3/examples/simple/
I fixed it by using normalize prop in Field for checkbox component like: <Field normalize={value => value || ''} ... />
. Another approach could be setting default initial values for all the checkboxes in each form, which doesn’t make sense at all.
I propose to set as a default behavior true || ‘’ for checkboxes. Then the checkboxes will behave the same way as any other types of inputs.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:18 (7 by maintainers)
Top GitHub Comments
This seems like a very bad solution to the problem. If your checkbox is initialised with an empty string as the value, isn’t the problem this initial wrong value rather than any subsequent correct values?
Since this breaking change was released as a minor version you probably very silently broke most applications using checkboxes in redux-form. It certainly broke mine.
Not sure if intended, but this change now breaks the application I’m currently working on. I’m submitting boolean values from my form to an API which expects the relevant property to be an actual boolean value (
true
/false
).However, after this change, when I uncheck the box, it now submits an empty string while previously it submitted
false
. The API cannot convert the empty string to a boolean, so I’m getting an error.I can work around this by converting the value to an actual boolean before making the API call, or fixing it on the API-side. I’m not really liking either approach though, since it affects every checkbox value I use within the app.