1.用useRef获取到form表单
2.使用form的方法,validateFields触发校验
const forms = useRef(null)
const [form] = Form.useForm()
// async 和 await 写法
const onFinish = async () =>{
const passed = forms.current.validateFields() //触发表单校验
if(passed){
.....
}
}
// 普通写法
const onFinish = () =>{
forms.current.validateFields().then(values => {
/*
values:
{
username: 'username',
password: 'password',
}
*/
})
.catch(errorInfo => {
/*
errorInfo:
{
values: {
username: 'username',
password: 'password',
},
errorFields: [
{ name: ['password'], errors: ['Please input your Password!'] },
],
outOfDate: false,
}
*/
});
}