实现效果图:
const rules= computed(()=>{
let obj = reactive({
receiver: [{ required: true, message: "必选项" }],
receiverMobile: [{ required: true, message: "", whitespace: true }]as any,
receiverPhone: [{ required: true, message: "", whitespace: true }]as any,
areaCode: [{ required: true, message: "必填项", type: "array" }],
address: [
{ required: true, message: "必填项", whitespace: true },
// { max: 30, message: '请在30个字符以内' }
],
zipCode: [{ max: 6, message: "请输入正确的邮编" }],
});
// 所选的地区如果是国外(不含港澳台),则手机号码为非必填,电话号码为必填
if(form.areaCode[0]==-30){
obj.receiverPhone=[{ required: false, message: "", whitespace: true }]
obj.receiverMobile=[{ required: true, message: "", whitespace: true },{validator:telRule}]
}else{
// 否则,则手机为必填,电话为非必填
obj.receiverPhone=[{ required: true, message: "", whitespace: true },{validator:telRule}]
obj.receiverMobile=[{ required: false, message: "", whitespace: true }]
}
return obj
})