validation fails after reinitialization
See original GitHub issueHi, first of all congrats to this great component. Great work!
I’m combining the two website examples “Initializing from State” and “Submit Validation”. I’m using the submitted data to reinitialize the form.
The form is correctly reinitialized with the submitted data. However, after reinitializing, the validation function is called with the old initialValues. I end up with sync errors, although the data is valid.
The action sequence is this:
@@redux-form/START_SUBMIT
@@test/UPDATE_DATA (here I'm updating the state, which is used as initialValues)
@@redux-form/INITIALIZE
(in between these 2 actions, the validation function is called, with the **old** initialValues, resulting in sync errors)
@@redux-form/UPDATE_SYNC_ERRORS
@@redux-form/STOP_SUBMIT
@@redux-form/SET_SUBMIT_SUCCEEDED
It seems that the wrong values are passed in the nextProps in validateIfNeeded: reduxForm.js#L164
- nextProps.initialValues is correctly set to the updated initalValues.
- The problem: nextProps.values equals to this.props.initialValues. nextProps.values should either be empty or equal to the updated initialValues
Code
const initialTestState = {
data: {
username: ''
}
};
const validate = values => {
const errors = {}
if (!values.username) {
errors.username = 'Required'
}
return errors;
};
function submit(values, dispatch) {
return sleep(1000) // simulate server latency
.then(() => {
dispatch(updateTestDataAction(values));
})
};
let SyncValidationForm = reduxForm({
form: 'syncValidation',
validate,
enableReinitialize: true
})(FormComponent);
SyncValidationForm = connect(
state => ({
initialValues: state.test.data
})
)
I’m using redux-form 6.4.3, but I encounter the same issue with 6.3 and 6.2.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:13
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Validation not working after clear the form - Stack Overflow
Hi you just rest your form like that: clearall(): void { this.isEditMode = false; this.stockForm.reset(); Object.keys(this.
Read more >Validation required field error after resetting the properties
I have an update user password form in my application which is working fine except when I reset the fields using $this->reset(['current_password', ...
Read more >Reactive Form - Reset input validation after change
It seems that changing the entity on the left still leaves the validation error in place - but quickly fixed by adding your...
Read more >How to reset value of an item after a failed validation?
And what do you mean by "reset"? To what? After you correct the items in error and re-submit the page, the value originally...
Read more >Rhino 5 Validation Fails - Even After Reset - McNeel Wiki
If validation fails even after Sales has reset your license, or you are prompted to enter an upgrade key and you do not...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m having the same issue. Validation stops working when
enableReinitialize
is set to true. Only after the field is edited manually the syncErrors appear, so a user can submit a form when initializedI have been looking at the code pertaining to
enableReinitialize
inside reduxForm.js of version 6.5.0, and it makes little sense to me. Below is my humble attempt at suggesting a basis for a fix: The following code:Has the following problems that I can see:
Why does the flag
shouldUpdateInitialValues
depend on whether there were initial values specified previously? Isn’t the only important thing is that we now have new initial values and that they are different than the old ones (including the case that the old ones were undefined)?Why does the
if (shouldUpdateInitialValues)
reuses the old initial values (found instateInitial
)? Wasn’t the meaning to actually test for the negation of this flag?Shouldn’t we update the
formState
with the new initial values (and the new values in case ofshouldResetValues
beingtrue
) so that code coming afterwards in the file (that, in particular, runs validation etc.) will use the new data? Can this be the root cause of the issue in this thread?In other words, shouldn’t the code look something like this:
I guess that after these changes, we can go ahead and simplify this code:
to this: