ant-design: Default value of input component (Select) is not used while submitting Form

  • I have searched the issues of this repository and believe that this is not a duplicate.

Reproduction link

Edit on CodeSandbox

Steps to reproduce

  • Click on submit button without selecting anything and check console (Form’s onFinish method’s data is logged)

What is expected?

The data received in onFinish should reflect the defaultValue prop value in Select component (that is also displayed to the user) { test: [1], test2: [], test3: [1] }

  • First the Select component must be initialised by the defaultValue provided.
  • Then if initialValues prop is provided in Form then the value in Select should be replaced by that value

What is actually happening?

The data received in onFinish is not initialised with defaultValues as shown in UI: {test: undefined, test2: undefined, test3: [1]}

Environment Info
antd 4.0.0-beta.0
React 16.12.0
System Mac OS
Browser Chrome (Version 78.0.3904.108)

This behaviour is also present in other form inputs.

The reason we are not using initialValues to set defaults is because our forms are dynamic and consists of different types of inputs (select, checkbox, etc) where each input type handles it’s own defaults.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 18 (9 by maintainers)

Most upvoted comments

When you add input item into the Form and Form.Item and set a name for Form.Item, the input item is completely controlled by the Form. You need to use the Form’s api to control the input item.

For the default value, you can use the initialValues in this case.

The solution you all are looking for is to set it in <Form initialValues={{ yourDesiredFormInputName: true }} ><Form.Item name="yourDesiredFormInputName" label="My Form Input"><Input /></Form.Item></Form>

What a stupid design!

There is problem in using initialValues. For e.g. if we are using Form.List that contains a complex form and we want some of the fields in that form to have some default value then we can’t assign it using initialValues in form because at that time we don’t know how many items will going to be added

You should use Form initialValues instead of defaultValue.

These Form.Item not bind data. It’s used for validate status demo.

@zombieJ

For dynamic fields, maybe we can extend add function to support init values

This does not solve the problem @piyushk96 mentioned

if we are using Form.List that contains a complex form and we want some of the fields in that form to have some default value then we can’t assign it using initialValues in form because at that time we don’t know how many items will going to be added