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

eNd*_*ddy 7 redux-form react-redux react-redux-form

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

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

似乎没有记录任何类型的afterAsyncValidation挂钩.以redux形式实现此目的的最佳方法是什么?

Eri*_* R. 1

redux-form设计的想法是您的数据将在提交时保存到服务器。

但是,没有什么可以阻止您.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)
Run Code Online (Sandbox Code Playgroud)