0

I am following redux form tutorial and almost done with that but could not pass the array in asyncValidate file

I need to change array ['john', 'paul', 'george', 'ringo'] with the array coming from server response...

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const asyncValidate = (values /*, dispatch */) => {
  return sleep(1000).then(() => {
    // simulate server latency
    if (['john', 'paul', 'george', 'ringo'].includes(values.username)) {
      throw { username: 'That username is taken' }
    }
  })
}

export default asyncValidate

1 Answer 1

1

your asyncValidate function needs to make an api call that returns you the list like

const asyncValidate = (values /*, dispatch */) => {
  return axios.get('url').then((data) => {
    if (data.includes(values.username)) {
      throw { username: 'That username is taken' }
    }
  })
}

export default asyncValidate
Sign up to request clarification or add additional context in comments.

2 Comments

If you pass it to the validate function, it no longer is an async validate, rather just validate.
I still could not find the correct way to implement async blur fields in redux form... can you please help me to do it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.