演示使用 validateFields() 实现分步验证、进度跟踪、步骤导航
const STEPS = [
{ id: 1, title: '账号信息',
fields: ['username', 'email', 'password'] },
{ id: 2, title: '个人资料',
fields: ['firstName', 'lastName', 'birthDate'] },
{ id: 3, title: '联系方式',
fields: ['phone', 'address', 'city'] }
];async function validateCurrentStep() {
const step = STEPS[currentStep - 1];
// 验证当前步骤的所有字段
const errors = await form.validateFields(step.fields);
if (Object.keys(errors).length > 0) {
// 标记字段为已触摸,显示错误
step.fields.forEach(field => {
form.setFieldTouched(field, true);
});
return false;
}
return true;
}async function nextStep() {
const isValid = await validateCurrentStep();
if (isValid && currentStep < STEPS.length) {
currentStep++;
}
}
function prevStep() {
if (currentStep > 1) {
currentStep--;
}
}