简体   繁体   English

Redux表单-按需验证-向导表单

[英]Redux-form - Validate on demand - wizard form

I'm using redux-form to build a multipage form. 我正在使用redux-form构建多页表单。

I have a multi "page" form. 我有一个多“页面”形式。 My parent form is initialize like this : 我的父母形式是这样初始化的:

Parents = reduxForm({
    form: 'my-form',
    enableReinitialize: true,
    destroyOnUnmount: false,
})(Parents);

And multiple component/page displayed conditionaly like this : 并有条件地显示多个组件/页面,如下所示:

{(step === 1) && <Intro onSubmit={this.handleSubmit} loading={submitting}/>}
{(step === 2) && <Motifs onSubmit={this.handleSubmit} onSubmitFail={this.handleSubmitFail} loading={submitting}/>}
{(step === 3) && <Fichiers onSubmit={this.handleSubmit} onSubmitFail={this.handleSubmitFail} loading={submitting}/>}
{(step === 4) && <Confirmation onSubmit={this.handleSubmit} onSubmitFail={this.handleSubmitFail} loading={submitting}/>}
{(step === 5) && <Terminee onSubmit={this.handleSubmit} onSubmitFail={this.handleSubmitFail} loading={submitting}/>}

this.handleSubmit is a function that call this.props.handleSubmit() and add or remove 1 to step if the server respond http 200. this.handleSubmit是一个函数,该函数调用this.props.handleSubmit()并在服务器响应http 200时在步骤中添加或删除1

On the components <Motifs> for example i initialize redux-form like this : 例如,在组件<Motifs> ,我像这样初始化redux-form:

Motifs = reduxForm({
    form: 'my-form',
    destroyOnUnmount: false,
    validate: validateMotifs // validation function
})(Motifs);

And for each of my form components/pages i have some button to previous/next like this : <NavFormButton loading={loading} handleSubmit={handleSubmit} onSubmit={onSubmit}/> 对于我的每个表单组件/页面,我都有一些上一个/下一个按钮,如下所示: <NavFormButton loading={loading} handleSubmit={handleSubmit} onSubmit={onSubmit}/>

When i click next i want to validate and submit the form. 当我单击下一步时,我要验证并提交表单。 It's working. 工作正常 But when i go previous i just want to submit the data so they are saved but don't want to validate because the user may have not fill all the fields. 但是当我上一步时,我只想提交数据以便保存它们,但又不想验证,因为用户可能没有填写所有字段。

How should i do ? 我应该怎么做 ? Am i following the good path ? 我是否遵循正确的道路?

Having a lot of trouble to understand how to do this correctly ... Thanks. 麻烦您了解如何正确执行此操作...谢谢。

First you can try to reinitialize form with the current values, submit and then go to the previous form. 首先,您可以尝试使用当前值重新初始化表单,提交并转到上一个表单。

dispatch(initialize('my-form', newInitialValues))

See more here 在这里查看更多

Also if you are using v6, then you should look shouldAsyncValidate function to run async validation when you need it. 另外,如果您使用的是v6,则应在需要时查看shouldAsyncValidate函数以运行异步验证。 Official docs 官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM