3

I'm wondering how to get async validation to trigger on a Field component inside of a FieldArray. I have something like:

class MyForm extends Component {
  constructor(props) {
    super(props)

  }

  render() {
    const { handleSubmit } = this.props

    return (
      <form onSubmit={handleSubmit}>  
        <Field
          name="name"
          type="text"
          component={RenderInputField}
        />
        <FieldArray
          name="hobbies"
          component={RenderHobbies}
        />
      </form>
    )
  }
}

MyFormBase = reduxForm ({
  form: 'MyForm',
  validate,
  asyncValidate,
  asyncBlurFields: ['name', 'hobbies.hobby']
})(MyFormBase)

With RenderHobbies as:

const RenderHobbies = ({fields}) => (
  <div>
    {fields.map((hobby, index) => ({
      <Field
        name={`${hobby}.hobby`}
        component={RenderInputField}
      />
    }))}
  </div>
)

export default RenderHobbies

This doesn't work. async validation will fire for "name" on blur but not "hobbies.hobby". What would the correct syntax for that be?

1 Answer 1

5

The syntax I was looking for was:

asyncBlurFields: ['hobbies[].hobby']

Pretty simple, I just couldn't find it anywhere in the docs. I found it by going through this thread

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

1 Comment

I guess a follow up question I have to that is how do I then access the index of that hobby in my asyncValidate function?

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.