Antd自定义表单验证 判断图片是否存在
const onCheck = () => {
props.form.validateFields()
.then((values) => {
let cur = {
type: props.type,
id: props.curData.id,
};
props.addSuc(values, cur);
})
.catch((err) => {
console.log('err', err);
});
};
const checkUrl = (rule, value) => {
return new Promise((resolve, reject) => {
if (!value) {
reject('请输入图片链接');
} else {
const img = new Image();
img.src = value;
img.onload = () => {
console.log('宽', img.width);
console.log('高', img.height);
if (!(img.width === 333 && img.height === 184)) {
reject('图片错误');
}
resolve();
};
img.onerror = () => {
reject('图片错误');
};
}
});
};
<Form.Item name="actPicture" rules={[{ validator: checkUrl }]}>
<Input placeholder="请输入活动图片" />
</Form.Item>