Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.2.0] Initial data passed to sync validate is undefined #2090

Closed
dmeenhuis opened this issue Nov 7, 2016 · 3 comments
Closed

[6.2.0] Initial data passed to sync validate is undefined #2090

dmeenhuis opened this issue Nov 7, 2016 · 3 comments

Comments

@dmeenhuis
Copy link
Contributor

dmeenhuis commented Nov 7, 2016

The changes in #1992 introduced a subtle change in the data that is passed to the sync validate function.

I have a form that uses enableReinitialize and sync validation. It also receives initialValues when connecting the component.

Simplified code example:

const validate = (values, props) => { /* Some validation rules */ };

const EditorForm = reduxForm({
  form: 'userForm',
  enableReinitialize: true,
  validate,
  asyncValidate
})(UserEditor);

export default connect<StateProps, DispatchProps, OwnProps>(
  (state: ApplicationState, ownProps: OwnProps) => ({
    initialValues: Object.assign({}, state.authorisation.userDetails, { companyId: ownProps.companyId })
  }),
  {}
)(EditorForm);

When the form is mounted initially, it also calls the validate function. Now, with version 6.1.1, the values argument contained the value provided by initialValues. In 6.2.0 however, this value is now undefined, causing a runtime error in my application.

I've pinpointed the cause to this specific line: src/reduxForm.js#L475

In my case the variable shouldUpdateInitialValues is set to true because I'm using enableReinitialize and initialValues differs from stateInitial (this value is undefined).

So in this case, initial is updated with stateInitial, therefore becoming undefined. This value is then passed to the validate function, causing an error.

A fix on my end is trivial, I could just assign a default value in the destructuring, but I'm wondering whether this is the intended behaviour, or that a subtle bug was just introduced?

I suppose from an API-point-of-view it perhaps seems more sensible to always pass at least an empty object as values to the validate function, in order to prevent these kind of weird runtime errors. As far as I can tell, this was how it worked in the previous version :)

@melinath
Copy link

melinath commented Nov 8, 2016

Yeah, just ran into this as well. Accidentally upgraded from 6.1.1 to 6.2.0 and all my validation functions broke.

erikras pushed a commit that referenced this issue Nov 11, 2016
* Values passed to the sync validation function must be defined. Closes #2090

* Fixed linting errors (removed semi-colons)
@erikras
Copy link
Member

erikras commented Nov 30, 2016

Published in v6.2.1.

@lock
Copy link

lock bot commented Jun 2, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants