redux-form - asyncBlurFields with FieldArray Compo

2019-08-04 06:02发布

问题:

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:

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