javascript - Warning: ant Checkbox: value is not a valid prop, do you mean checked? - Stack Overflow

While working with Form in ant design v4, I have difficulty with using CheckBox inside Form.Item.const

While working with Form in ant design v4, I have difficulty with using CheckBox inside Form.Item.

const [form] = useForm();

useEffect(() => {
    form.setFieldsValue({
        title: '',
        billable: false,
    })
}, []);

const onFieldsChange = (changedFields, allFields) => {
    console.log('onFieldsChange.changedFields => ', changedFields);
    console.log('onFieldsChange.allFields=> ', allFields);
}

return (
    <Form form={form} onFieldsChange={onFieldsChange}>
        <Form.Item label="title" name="title">
            <Input />
        </Form.Item>
        <Form.Item label="Billable" name="billable">
            <CheckBox />
        </Form.Item>
    </Form>
);

Above code gives me the following error:

Warning: [ant:Checkbox] value is not a valid prop, do you mean checked?

How can I use CheckBox inside the Form.Item in ant design v4?

While working with Form in ant design v4, I have difficulty with using CheckBox inside Form.Item.

const [form] = useForm();

useEffect(() => {
    form.setFieldsValue({
        title: '',
        billable: false,
    })
}, []);

const onFieldsChange = (changedFields, allFields) => {
    console.log('onFieldsChange.changedFields => ', changedFields);
    console.log('onFieldsChange.allFields=> ', allFields);
}

return (
    <Form form={form} onFieldsChange={onFieldsChange}>
        <Form.Item label="title" name="title">
            <Input />
        </Form.Item>
        <Form.Item label="Billable" name="billable">
            <CheckBox />
        </Form.Item>
    </Form>
);

Above code gives me the following error:

Warning: [ant:Checkbox] value is not a valid prop, do you mean checked?

How can I use CheckBox inside the Form.Item in ant design v4?

Share Improve this question edited Mar 15, 2021 at 7:22 Ajeet Shah 19.9k9 gold badges64 silver badges104 bronze badges asked Mar 15, 2021 at 2:54 Josh ThomasJosh Thomas 1,6873 gold badges11 silver badges23 bronze badges 1
  • There is an example in the docs, here. – maazadeeb Commented Mar 15, 2021 at 7:28
Add a ment  | 

2 Answers 2

Reset to default 6

Use valuePropName for Checkbox form item:

<Form.Item label="Billable" name="billable" valuePropName="checked">
  <Checkbox />
</Form.Item>

Default value of valuePropName is value which is correct for input fields but not for a checkbox or switch.

The accepted answer didn't work for me so I had to use the customer which was given in the docs.

<Form.Item
          name="termsAccepted"
          valuePropName="checked"
          rules={[
            {
              validator: (_, value) =>
                value
                  ? Promise.resolve()
                  : Promise.reject(new Error("Should accept agreement")),
            },
          ]}
        >
          <Checkbox checked={Form.useWatch("IsCurrentRecord", form)}>
            I agree that I have read and accepted
          </Checkbox>
        </Form.Item>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435623a4627594.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信