title: ant design的form的validate需要注意的点
date: 2023-10-16
author: KazooTTT
tags:
- ant design
published: true
ant design的form的validate需要注意的点
<Form.Item
name="confirm"
label="Confirm Password"
dependencies={['password']}
hasFeedback
rules={[
{
required: true,
message: 'Please confirm your password!',
},
({ getFieldValue }) => ({
validator(_, value) {
if (!value || getFieldValue('password') === value) {
return Promise.resolve();
}
return Promise.reject(new Error('The new password that you entered do not match!'));
},
}),
]}
>
<Input.Password />
</Form.Item>
需要注意一下,即使rule中存在
{
required: true,
message: 'Please confirm your password!',
},
在后面的自定义校验(validator)中,也需要对value进行判断是否非空。
否则容易抛出其他的异常。