shackra
shackra

Reputation: 366

How to consult value of field with ant-design useForm?

I have this form which I would like to show or hide fields depending on the value of other field(s) that are fix (they never hide) but using form.getFieldValue("some-field") always return undefined.

Here is a minimal functional example that showcase the issue I have:

Edit loving-almeida-tb1h2

What am I doing wrong with antd and how can I achieve my use case?

Upvotes: 1

Views: 3893

Answers (1)

Triyugi Narayan Mani
Triyugi Narayan Mani

Reputation: 3109

By looking into the official documentation, they have provided a way to use conditions inside <Form.Item>.

<Form.Item
  noStyle
  shouldUpdate={(prevValues, currentValues) => prevValues.gender !== currentValues.gender}
>
  {({ getFieldValue }) =>
    getFieldValue('gender') === 'other' ? (
      <Form.Item
        name="customizeGender"
        label="Customize Gender"
        rules={[
          {
            required: true,
          },
        ]}
      >
        <Input />
      </Form.Item>
    ) : null
  }
</Form.Item>

I have changed your example to work as expected. Please check.

Edit blue-platform-06fji

Upvotes: 5

Related Questions