React:如何使用子FormItem组件而不获取警告:validateDOMNesting:<form>不能作为后代出现

时间:2018-02-05 09:31:51

标签: forms reactjs nested-forms

鉴于父组件,我使用的子组件DynamicFieldSetFormItems的分组。但我收到错误:

Warning: validateDOMNesting(...): <form> cannot appear as a descendant of <form>. See CreateTopic > Form > form > ... > DynamicFieldSet > Form > form.

我尝试删除子组件中的<Form> </Form>标记,但这是编译错误。

有没有办法可以禁用子窗体视图的渲染?

父组件

class CreateTopic extends React.Component {
render() {
    return (
      <div className="create-topic-container">
        <h3>Create an event</h3>
        <Form onSubmit={this.handleSubmit}>
          <FormItem>...</FormItem>
          <FormItem>...</FormItem>
          <FormItem>...</FormItem>
          <FormItem
            {...formItemLayout}
            label="Results"
            style={{ marginBottom: SPACING_FORM_ITEM }}
          >
            {getFieldDecorator('results', {
              rules: [
                {
                  required: true,
                  message: 'Results cannot be empty.',
                },
              ],
            })(<DynamicFieldSet
              form={this.props.form}
            />)}
          </FormItem>
        </Form>
      </div>
    );
  }
}

DynamicFieldSet - 子组件

export class DynamicFieldSet extends React.Component {
  render() {
    getFieldDecorator('keys', { initialValue: ['0', '1'] });
    const keys = getFieldValue('keys');
    const formItems = keys.map((k, index) => {
      return (
        <FormItem
          {...formItemLayoutWithOutLabel}
          required={false}
          key={k}
        >
          {getFieldDecorator(`results[${k}]`, {
            validateTrigger: ['onChange', 'onBlur'],
            rules: [
              {
                required: true,
                whitespace: true,
                message: 'Result name cannot be empty.',
              },
              {
                validator: this.validateLength,
              },
            ],
          })(<Input placeholder={`Result #${index + 1}`} style={{ width: '80%', marginRight: 8 }} />)}
          {keys.length > 2 ? (
            <Icon
              className="dynamic-delete-button"
              type="minus-circle-o"
              disabled={keys.length === 1}
              onClick={() => this.remove(k)}
            />
          ) : null}
        </FormItem>
      );
    });

    return (
      <Form>
        {formItems}
        <FormItem {...formItemLayoutWithOutLabel}>
          {keys.length < 10 ? (
            <Button type="dashed" onClick={this.add} style={{ width: '80%' }}>
              <Icon type="plus" />Add Result
            </Button>
          ) : null}
        </FormItem>
      </Form>
    );
  }
}

3 个答案:

答案 0 :(得分:2)

我在使用ant design table时遇到了这个问题,并发现它的非蚂蚁设计引发了警告。这是网络标准description

"Every form must be enclosed within a FORM element. There can be several forms in a single document, but the FORM element can't be nested."

因此,表单标签内不应有表单标签。

要解决此问题(在我们的示例中),请删除DynamicFieldSet“返回”中的Form标签,并替换为div标签

希望它会有所帮助:)

答案 1 :(得分:0)

您可以门户输入如下表格:

deleted_at

在上面的示例中,我使用react material-ui Portal component。但是您也可以尝试使用React Portals来实现它

答案 2 :(得分:0)

这是由于您在另一个<form>标记内声明了<form>而引起的。