question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getFieldDecrator register strangely

See original GitHub issue

Version

3.1.6

Environment

macOS 10.11.6, Chrome 63.0.3239.132

Reproduction link

https://codesandbox.io/s/o7l8nxyyxy

Steps to reproduce

click add button more than one time

What is expected?

the data get by getFieldsValue is always an array.

What is actually happening?

the data got is a object after the second click


There is a workaroud can avoid the problem, but it seems inelegant: getFieldsDecorator a new key such as tempConditions with a copy data. Is it a bug of antd or is it a usage error ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

27reactions
nodkrotcommented, Feb 10, 2019

Dynamic Form Item example from https://ant.design/components/form/

    ...
    getFieldDecorator('keys', { initialValue: [] });
    const keys = getFieldValue('keys');
    const formItems = keys.map((k, index) => (
      <Form.Item
        {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
        label={index === 0 ? 'Passengers' : ''}
        required={false}
        key={k}
      >
        {getFieldDecorator(`names[${k}]`, {
          validateTrigger: ['onChange', 'onBlur'],
          rules: [{
            required: true,
            whitespace: true,
            message: "Please input passenger's name or delete this field.",
          }],
        })(
          <Input placeholder="passenger name" style={{ width: '60%', marginRight: 8 }} />
        )}
        ...
      </Form.Item>
...

What doesn’t make sense to me is, why do we need two different fields getFieldDecorator('keys', { initialValue: [] }); => keys and getFieldDecorator('names[${k}]', { => names? When submitting the form the data is duplicated:

{
    ...
    keys: [{},{}],
    names: [{},{}],
    ...
}

Instinct tells me I should be able to do getFieldDecorator('names', { initialValue: [] }); and getFieldValue('names') but then I get this error:

One field name cannot be part of another, e.g. a and a.b.

What am I missing here?

0reactions
bishnu476commented, Aug 6, 2020

This works perfectly for me though if the comparator is used the same warning pop up only at the start if the comparator is not used (which is used only to make id value to list size at initially ) no warning will be displayed. [Solution here

](https://codesandbox.io/s/duplicate-form-j7qso?file=/index.js:1585-3123) //Initial value of the keys in the list getFieldDecorator(“keys”, { initialValue: [0, 1, 2] });

//Initial values of the list const initialNames = [“A”, “B”, “C”];

// const keys = getFieldValue(“keys”);

// Array Comporator const arrayEquals = (a, b) => { return ( Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((val, index) => val === b[index]) ); };

// Comporator that will only true initially and set id value to arrays.length-1 if (getFieldValue(“keys”).length === initialNames.length) { if (!arrayEquals(getFieldValue(“names”), initialNames)) { id = getFieldValue(“keys”).length - 1; console.log(“id”, id); } }

const formItems = keys.map((k, index) => (
  <Form.Item
    {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
    label={index === 0 ? "Passengers" : ""}
    required={false}
    key={k}
  >
    {getFieldDecorator(`names[${k}]`, {
      initialValue: k + 1 <= initialKey.length ? `${initialNames[k]}` : " ",  //values initialized here from initialNames
      validateTrigger: ["onChange", "onBlur"],
      rules: [
        {
          required: true,
          whitespace: true,
          message: "Please input passenger's name or delete this field."
        }
      ]
    })(
      <Input
        placeholder="passenger name"
        style={{ width: "60%", marginRight: 8 }}
      />
    )}
    {keys.length > 1 ? (
      <Icon
        className="dynamic-delete-button"
        type="minus-circle-o"
        onClick={() => this.remove(k)}
      />
    ) : null}
  </Form.Item>
));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Antd getFieldDecorator()'s options trigger not working
When I add the trigger: 'onBlur' the Input component doesn't show the text that i enter. The idea is for Antd to collect...
Read more >
React ant design form validation not working (on submit)-Reactjs
I create a build of my react app and connected with electron. · I am trying to make an API request based on...
Read more >
How to show error message when someone leave empty fields
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are...
Read more >
TypeScript repositories for a bounty hunter | by IssueHunt
IssueHunt is an issue-based bounty platform for open source projects. Since launched in July 2018, a lot of projects have been participating to...
Read more >
Form E - OSCHINA - 中文开源技术交流社区
... { getFieldDecorator } = this.props.form; return ( <Form onSubmit={this. ... className="login-form-button"> Log in </Button> Or <a href="">register now!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found