Is there a way to normalize values of a ant design input on submit?

Viewed 4397

I'm using Ant Design with React JS, and I use the Form.Item normalize prop to parse the value that came from api to input.

I want to do the inverse now, normalize from input to api format on submit.

For example: I have a input called cash, that receive a float and normalize to currency, but on submit form I'd like to receive the value in float again, is there possible? I search in github issues but find nothing related.

<Form onSubmit={values => send(values)}> // I'd like retrieve values.cash in float without parse directly every time
  <Input normalize={normalize_to_currency} name="cash" />
</Form>

PS: I'm using non-controlled ant design form.

1 Answers

You can use Form.Item normalize props

<Form onFinish={values => console.log(values)}>
    <Form.Item name="currecny" normalize={e => parseFloat(e)}>
      <Input placeholder="Currency" type="number" />
    </Form.Item>
    <Button htmlType="submit">Submit</Button>
</Form>

You can check this sandbox https://codesandbox.io/s/cranky-resonance-mtkxi

If you are using Ant Design 3.x in getFieldDecoder you will get normalize props

Related