简体   繁体   中英

Redux-form validation based on field in other form

I have 2 forms, in which the validation for a field in the second form is based on the value of a field in the first form. This works as expected when filling in the form top-down. However, when I change the value in the first form, the values object isn't updated in the validation.

My validate-function looks something like this:

const validate = values => {
  const errors = {}
  if (!values.username) {
    errors.username = 'Required'
  } else if (values.username.length < values.previous_field.length) {
    errors.username = 'Your name can't be shorter than the previous field';
  }

  return errors;
}

When I change the previous field to a short value after filling in a valid username, the username-field never invalidates.

I decided to rename the forms so then would both have the same name. In this way the values do get updated, and you can use the validation in the way I would like.

Both forms are now named userdata , and the first one has all the validation logic:

@reduxForm({
  form: 'userdata',
  validate,
  asyncValidate,
  asyncBlurFields: ['car_licenceplate', 'user_zipcode']
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM