redux-form: Validation errors not passed to fields after re-initialize when enableReinitialize is turned on
When passing the enableReinitialize
flag all of the fields are getting being marked as valid even though there are errors present for them. It appears to be that when initialize is called a second time it does not trigger the redux-form/UPDATE_SYNC_ERRORS
action and as such all of the fields don’t get updated with their error state again.
When this flag is passed, I think the redux-form/UPDATE_SYNC_ERRORS
action always needs to get triggered in this scenario and maybe a change in the logic of updateSyncErrorsIfNeeded
is needed?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 17
- Comments: 40 (6 by maintainers)
Inside componentWillMount,
reset
the form component and setdestroyOnUnmount
to false. Also Initialize the fields as empty string or whatever you need.The problem in my case was that when the component unmounted, the formState got destroyed and on reinitialization, the action
redux-form/UPDATE_SYNC_ERRORS
wasn’t getting dispatched. Now it’s solved.I guess, things I’m experiencing are related to this issue. An events chronicle:
@lechneal I had a similar issue that I resolved with this code.
Same issue when your initial values are invalid, you do some changes to fields not required (that won’t affect
syncErrors
) and you pressreset
. Thenredux-form/UPDATE_SYNC_ERRORS
event is not triggered because!plain.deepEqual(syncErrors, nextSyncErrors)
returnfalse
(this is because you are comparingsyncErrors
coming fromthis.props
while there are currentlynextProps
that have up to datesyncErrors
array and this one should be used for that comparison).Definitely
validateIfNeeded
andupdateSyncErrorsIfNeeded
need to be fixed because its not working in v6.0.1 too.If the initial data is initialized using the action creator - initialize(in method componentWillMount), rather than using the props initialValues, then when second call initialize - it does not trigger the redux-form/UPDATE_SYNC_ERRORS action and as such all of the fields don’t get updated with their error state again.
v6.8.0
When debugging this issue I noticed the
props.values
andnextProps.values
that are passed toshouldValidate
(https://github.com/erikras/redux-form/blob/master/src/defaultShouldValidate.js) are always the same. Not sure why this happens sincevalidateIfNeeded
is called oncomponentWillReceiveProps
.Since both values are always the same,
shouldValidate
only returns true when it’s an initialRender.For now I’m adding
keepDirtyOnReinitialize: true
but it’s not really the ideal behavior for meMy way to resolve this problem is to call reset() manually when initialValues got changes. But you should not disable
enableReinitialize
, because, without it, component won’t receiveinitialValues
prop as expected.The important condition is to check whether this.props.initialValues is not null, if you are using async initialization.
Do you have a workaround for this issue?
Same here, any progress on this issue?