Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Antd switch not triggering onValuesChange when toggling?

I have a basic form:

<Form
  layout="vertical"
  form={form}
  onFinish={onSubmit}
  onValuesChange={onValueChanges}
>
  <Form.Item
    label="Foo"
    name="requirePassword"
    valuePropName="checked"
  >
    <Switch />

        {requirePasswordSettingDirty && (
          <span className={styles.requirePasswordWarning}>
            Required password
          </span>
        )}
  </Form.Item>
</Form>

When I click the switch, it doesn’t trigger the callback onValueChanges. What am I missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Data binding is tied to a Form.Item with name key and wraps only an Input. Therefore if you have additional elements alongside the Input, such as a custom label element, you’ll have to nest Form.Items.

 <Form.Item label="Foo" valuePropName="checked">
    <Form.Item name="requirePassword">
      <Switch />
    </Form.Item>
    {requirePasswordSettingDirty && <span>Required password</span>}
  </Form.Item>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading