异步验证后上载Redux-Form文件

时间:2016-12-16 18:23:18

标签: react-redux redux-form react-redux-form

我有一个通过redux-form字段连接的FileUpload组件。在输入字段中选择文件时,它将input.onChange和input.onBlur调用所选文件作为base64字符串。

我正在使用asyncValidator redux-form选项来验证图像的尺寸,我希望在异步验证完成后将文件上传到我的服务器。

似乎没有记录任何类型的afterAsyncValidation挂钩。在redux-form中实现此目的的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

redux-form的设计理念是将您的数据保存到服务器 submit

但是,在您执行异步验证之后,没有什么能阻止您放置自己的.then()子句。像这样的东西?

// async function you already have that is looking at your
// picture field and rejecting the promise with errors
import validateDimensions from './validateDimensions'

// async function to upload the image
import upload from './uploadImage'

const MyForm = reduxForm({
  form: 'myForm',
  asyncValidate: values =>
    validateDimensions()
      .then(() => {
        // we know validation passed
        upload(values.prettyPicture)
        // ^ not "return upload(values.prettyPicture)" to let this async
        // validation promise resolve and do upload asynchronously
        // after it has resolved
      })
  },
  asyncBlurFields: [ 'prettyPicture' ]
})(MyForm)