redux-form - 具有FieldArray组件的asyncBlurFields

时间:2017-09-12 20:43:11

标签: reactjs redux-form

我想知道如何在FieldArray内的Field组件上触发异步验证。我有类似的东西:

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)

使用RenderHobbies:

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

export default RenderHobbies

这不起作用。异步验证将触发&#34; name&#34;在模糊但不是&#34; hobbies.hobby&#34;。它的正确语法是什么?

1 个答案:

答案 0 :(得分:2)

我正在寻找的语法是:

asyncBlurFields: ['hobbies[].hobby']

非常简单,我只是无法在文档中的任何地方找到它。我通过this线程

找到了它