Search code examples
javascriptreactjsantd

Setting initial values to dynamic form in antd 4


I have a problem while creating dynamic form in antd4, following this example from docs.

I want to use dynamic form to edit object which already has some values.

In this sandbox I created a new array of initial values: https://codesandbox.io/s/dynamic-form-item-ant-design-demo-slm8o?file=/index.js:636-649

The goal is to achieve this state when opening form:

enter image description here

Grateful for any help.


Solution

  • You need to pass an object to initialValues prop, that contains an array of values for names field list, like so:

        <Form
          name="dynamic_form_item"
          initialValues={{ names: ['', '', ''] }}
          {...formItemLayoutWithOutLabel}
          onFinish={onFinish}
        >
            <Form.List name="names">
                { /* your array of fields goes here */ }
            </Form.List>
        </Form>