diff --git a/src/views/mes/repairItems/RepairItemsForm.vue b/src/views/mes/repairItems/RepairItemsForm.vue index 3c990f83..dac9bfe0 100644 --- a/src/views/mes/repairItems/RepairItemsForm.vue +++ b/src/views/mes/repairItems/RepairItemsForm.vue @@ -95,6 +95,9 @@ const formLoading = ref(false) const formType = ref('') const formRef = ref() +const isInitializing = ref(false) +const isSwitchingDeviceType = ref(false) + const formData = ref({ id: undefined as number | undefined, subjectCode: undefined as string | undefined, @@ -163,15 +166,16 @@ const loadComponentOptionsByDeviceId = async (deviceId: number) => { watch( () => formData.value.deviceType, - (val) => { - if (val === 1) { - formData.value.componentId = undefined - componentOptions.value = [] - } else if (val !== 2) { - formData.value.deviceId = undefined - formData.value.componentId = undefined - componentOptions.value = [] - } + async () => { + if (isInitializing.value) return + isSwitchingDeviceType.value = true + formData.value.deviceId = undefined + deviceOptions.value = [] + formData.value.componentId = undefined + componentOptions.value = [] + await nextTick() + formRef.value?.clearValidate?.(['deviceId', 'componentId']) + isSwitchingDeviceType.value = false } ) @@ -187,6 +191,10 @@ watch( ) const validateDeviceId = (_: any, value: any, callback: any) => { + if (isSwitchingDeviceType.value) { + callback() + return + } const dt = formData.value.deviceType if ((dt === 1 || dt === 2) && (value === undefined || value === null || value === '')) { callback(new Error('设备不能为空')) @@ -196,6 +204,10 @@ const validateDeviceId = (_: any, value: any, callback: any) => { } const validateComponentId = (_: any, value: any, callback: any) => { + if (isSwitchingDeviceType.value) { + callback() + return + } const dt = formData.value.deviceType if (dt === 2 && (value === undefined || value === null || value === '')) { callback(new Error('关键件不能为空')) @@ -205,18 +217,19 @@ const validateComponentId = (_: any, value: any, callback: any) => { } const formRules = reactive({ - subjectCode: [{ required: true, message: '项目编码不能为空', trigger: 'blur' }], - subjectName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }], - deviceType: [{ required: true, message: '设备类型不能为空', trigger: 'change' }], - deviceId: [{ validator: validateDeviceId, trigger: 'change' }], - componentId: [{ validator: validateComponentId, trigger: 'change' }], - isEnable: [{ required: true, message: '是否启用不能为空', trigger: 'change' }] + subjectCode: [{ required: true, message: '项目编码不能为空' }], + subjectName: [{ required: true, message: '项目名称不能为空' }], + deviceType: [{ required: true, message: '设备类型不能为空' }], + deviceId: [{ validator: validateDeviceId }], + componentId: [{ validator: validateComponentId }], + isEnable: [{ required: true, message: '是否启用不能为空' }] }) const open = async (type: string, row?: any) => { dialogVisible.value = true dialogTitle.value = t('action.' + type) formType.value = type + isInitializing.value = true resetForm() if (!dictReady.value) { await dictStore.setDictMap() @@ -249,6 +262,9 @@ const open = async (type: string, row?: any) => { } } } + await nextTick() + formRef.value?.clearValidate?.() + isInitializing.value = false } defineExpose({ open })