简体   繁体   English

单击时验证最终表单数组

[英]Validating Final Form Array on Click

I am building an application with React Final Form.我正在使用 React Final Form 构建一个应用程序。 In the form, user needs to fill up basic input fields as well as add questions with its choices.在表单中,用户需要填写基本输入字段并添加问题及其选择。 For the questions and choices I am using FieldArray feature.对于问题和选择,我使用FieldArray功能。 So, until here everything is good and working.所以,直到这里一切都很好并且工作正常。 However, I would like to add another functionality to this form.但是,我想向此表单添加另一个功能。

As you can see in the image below, this is a Card component rendered inside FieldArray .如下图所示,这是在FieldArray呈现的 Card 组件。 Every time user clicks Add a Question button, There will be another Card component on the page.每次用户点击添加问题按钮,页面上都会出现另一个卡片组件。

The feature I need is to make the Save button work on top-right corner.我需要的功能是使Save按钮在右上角工作。 At the moment I don't know how should I implement the Save button but what I wanna achieve is that I want to toggle the Card component in the image to another Card component, where I display the input data by using fields.value .目前我不知道应该如何实现 Save 按钮,但我想要实现的是我想将图像中的 Card 组件切换到另一个 Card 组件,在那里我使用fields.value显示输入数据。 So, no input field.所以,没有输入字段。 However, I want to also validate this portion of the form when I click save.但是,我还想在单击保存时验证表单的这一部分。 This is what I don't know how to do.这是我不知道该怎么做。 So, every Save button will validate its own fields and if validation passes, the Card will be toggled to another Card component where the data is read-only.因此,每个 Save 按钮都会验证自己的字段,如果验证通过,则 Card 将切换到另一个数据为只读的 Card 组件。

So, I need your suggestion for the validation part as well as your opinion to add this functionality.因此,我需要您对验证部分的建议以及您添加此功能的意见。

Thanks谢谢

在此处输入图片说明

Edit编辑

I've been reading the docs of FinalForm as well as ReduxForm to figure out how can I handle this situation but I couldn't figure it out yet.我一直在阅读 FinalForm 和 ReduxForm 的文档以弄清楚如何处理这种情况,但我还没有弄清楚。

I've checked the Wizard example in FinalForm docs.我已经检查了 FinalForm 文档中的向导示例。 However I am not sure if it's suitable for my situation.但是我不确定它是否适合我的情况。 Wizard has a single <form> tag present at all times on a page and when a user clicks next button, input fields switch.向导在页面上始终存在一个<form>标记,当用户单击下一步按钮时,输入字段会切换。 In my case, I might need multiple form tags as you mentioned.就我而言,我可能需要您提到的多个表单标签。

I've put 3 form structures as an example.我以 3 个表单结构为例。 Can you tell me which way is to go?你能告诉我该走哪条路吗?

import { Form as FinalForm } from 'react-final-form'

#1 Basic Form: #1 基本形式:

Classic way of structuring the form and it is the current situation.结构形式的经典方式,这是目前的情况。 So, this is not the way to solve the issue.所以,这不是解决问题的方法。

      <FinalForm onSubmit={aCustomSubmitFunction}>
        {
          ({ handleSubmit }) => {
            return (
              <form onSubmit={handleSubmit}>
                <Field name='firstName'>{({input}) => <input {...input} />}</Field>
                <Field name='lastName'>{({input}) => <input {...input} />}</Field>
                <FieldArray name='questions'>
                  {({ fields }) => (
                    <div>
                      {fields.map((name, index) => {
                        return (
                          <div className="card">
                            <Field name={`${name}.question`}>{({input}) => <input {...input} type="text" />}</Field>
                            <button type="button" onClick={() => fields.remove(index)}>Delete</button>
                            <button type="submit">Save</button>
                          </div>
                        )
                      })}
                      <button type="button" onClick={() => fields.push({ question: undefined })}>Add a Question</button>
                    </div>
                  )}
                </FieldArray>
                <button type="submit">Submit</button>
              </form>
            )
          }
        }
      </FinalForm>

#2 Multiple forms under a single FinalForm: #2 单个 FinalForm 下的多个表单:

Multiple forms inside a FinalForm. FinalForm 中的多个表单。 This seems to be the way to go, however 'save' button submits the entire form not its own form.这似乎是要走的路,但是“保存”按钮提交的是整个表单而不是它自己的表单。 It is using the same handleSubmit so this must be the reason, though how can I have a different handleSubmit?它使用相同的handleSubmit所以这一定是原因,但我怎么能有不同的 handleSubmit? Wrapping the form tag that is inside FieldArray with another FinalForm ?用另一个FinalForm包装 FieldArray 内的表单标签?

      <FinalForm onSubmit={aCustomSubmitFunction}>
        {
          ({ handleSubmit }) => {
            return (
              <>
                <form onSubmit={handleSubmit}>
                  <Field name='firstName'>{({input}) => <input {...input} />}</Field>
                  <Field name='lastName'>{({input}) => <input {...input} />}</Field>
                  <button type="submit">Submit</button>
                </form>
                <FieldArray name='questions'>
                  {({ fields }) => (
                    <div>
                      {fields.map((name, index) => {
                        return (
                          <form onSubmit={handleSubmit}>
                            <div className="card">
                              <Field name={`${name}.question`}>{({input}) => <input {...input} type="text" />}</Field>
                              <button type="button" onClick={() => fields.remove(index)}>Delete</button>
                              <button type="submit">Save</button>
                            </div>
                          </form>
                        )
                      })}
                      <button type="button" onClick={() => fields.push({ question: undefined })}>Add a Question</button>
                    </div>
                  )}
                </FieldArray>
              </>
            )
          }
        }
      </FinalForm>

#3 Multiple nested forms under a single FinalForm: #3 单个 FinalForm 下的多个嵌套表单:

This is invalid html.这是无效的 html。 So this must be wrong approach but while I was doing a research I found a thing called React Portals, which might be helpful but I think it's unnecessary.所以这一定是错误的方法,但在我进行研究时,我发现了一个叫做 React Portals 的东西,它可能会有所帮助,但我认为这是不必要的。

      <FinalForm onSubmit={aCustomSubmitFunction}>
        {
          ({ handleSubmit }) => {
            return (
              <form onSubmit={handleSubmit}>
                <Field name='firstName'>{({input}) => <input {...input} />}</Field>
                <Field name='lastName'>{({input}) => <input {...input} />}</Field>
                <FieldArray name='questions'>
                  {({ fields }) => (
                    <div>
                      {fields.map((name, index) => {
                        return (
                          <form>
                            <div className="card">
                              <Field name={`${name}.question`}>{({input}) => <input {...input} type="text" />}</Field>
                              <button type="button" onClick={() => fields.remove(index)}>Delete</button>
                              <button type="submit">Save</button>
                            </div>
                          </form>
                        )
                      })}
                      <button type="button" onClick={() => fields.push({ question: undefined })}>Add a Question</button>
                    </div>
                  )}
                </FieldArray>
                <button type="submit">Submit</button>
              </form>
            )
          }
        }
      </FinalForm>

To validate only part of a form, you must split it into multiple forms, and have the "Save" button "submit" the form.要仅验证表单的一部分,您必须将其拆分为多个表单,并使用“保存”按钮“提交”表单。 The Wizard Example does this, collecting the form values from each "page" in a parent component.向导示例执行此操作,从父组件中的每个“页面”收集表单值。

Hope this helps...希望这可以帮助...

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

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