diff --git a/src/components/forms/DynamicForm.tsx b/src/components/forms/DynamicForm.tsx index d3901e1..4961f89 100644 --- a/src/components/forms/DynamicForm.tsx +++ b/src/components/forms/DynamicForm.tsx @@ -273,12 +273,6 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: const handleFieldChange = (fieldId: string, newVal: unknown, rawRow?: any) => { setSubmitError(null); - setBackendErrors(prev => { - if (!prev[fieldId]) return prev; - const next = { ...prev }; - delete next[fieldId]; - return next; - }); setValues(prev => { const next = { ...prev, [fieldId]: newVal }; @@ -364,6 +358,10 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: } } + if (typeof fillVal === 'string' && fillVal.trim() === '') { + fillVal = ''; + } + console.log(`[Auto-fill] Mapping _raw key "${key}" to form field "${targetCol.id}" with value:`, fillVal); next[targetCol.id] = fillVal; } else if (!targetCol && key !== fieldId) { @@ -407,6 +405,20 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: else next['total_kgs'] = totalKgs; } + const changedKeys = Object.keys(next).filter(k => next[k] !== prev[k]); + + setBackendErrors(errPrev => { + let errNext = { ...errPrev }; + let modified = false; + for (const k of changedKeys) { + if (errNext[k]) { + delete errNext[k]; + modified = true; + } + } + return modified ? errNext : errPrev; + }); + return next; }); }; @@ -514,11 +526,15 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: payload[f.id] = { lat: (val as any).lat, lng: (val as any).lng }; } else if (f.data_type === 'phone' && typeof val === 'string') { const phoneNum = val.replace(/^\+91\s*/, '').trim(); - payload[f.id] = { - dial_code: '+91', - phone: phoneNum, - phone_with_dial_code: `+91${phoneNum}` - }; + if (!phoneNum) { + continue; + } else { + payload[f.id] = { + dial_code: '+91', + phone: phoneNum, + phone_with_dial_code: `+91${phoneNum}` + }; + } } else if (f.data_type === 'image' || f.data_type === 'file') { const fileList = Array.isArray(val) ? val : [val]; const uploadedFiles = []; @@ -537,6 +553,10 @@ export function DynamicForm({ client, activityId: initialActivityId, instanceId: } payload[f.id] = uploadedFiles; } else { + if (f.data_type.startsWith('email') && typeof val === 'string' && val.trim() === '') { + continue; + } + if (f.data_type === 'grid' && Array.isArray(val)) { val = val.map(row => { const newRow = { ...row }; diff --git a/src/components/forms/LogVisitForm.tsx b/src/components/forms/LogVisitForm.tsx index e879347..0ba52fc 100644 --- a/src/components/forms/LogVisitForm.tsx +++ b/src/components/forms/LogVisitForm.tsx @@ -372,30 +372,32 @@ export function LogVisitForm({ client, onSuccess, onCancel, onActivityChange }: const handleFieldChange = (fieldId: string, val: any) => { setSubmitError(null); - setBackendErrors(prev => { - if (!prev[fieldId]) return prev; - const next = { ...prev }; - delete next[fieldId]; - return next; - }); setValues(p => { const next = { ...p, [fieldId]: val }; if (fieldId === 'date_of_visit') next.date_of_visit = val; if (fieldId === 'time_of_visit') next.time_of_visit = val; if (fieldId === 'upload_image') next.upload_image = val; valuesRef.current = next; + + const changedKeys = Object.keys(next).filter(k => next[k] !== p[k]); + setBackendErrors(errPrev => { + let errNext = { ...errPrev }; + let modified = false; + for (const k of changedKeys) { + if (errNext[k]) { + delete errNext[k]; + modified = true; + } + } + return modified ? errNext : errPrev; + }); + return next; }); }; const handleStoreSelect = (fieldId: string, val: string) => { setSubmitError(null); - setBackendErrors(prev => { - if (!prev[fieldId]) return prev; - const next = { ...prev }; - delete next[fieldId]; - return next; - }); const selectedOpt = storeOptions.find(o => String(o.value) === String(val)); const rawRow = selectedOpt?._raw || {}; @@ -404,10 +406,28 @@ export function LogVisitForm({ client, onSuccess, onCancel, onActivityChange }: // Map raw row fields into values Object.keys(rawRow).forEach(key => { - next[key] = rawRow[key]; + let val = rawRow[key]; + if (typeof val === 'string' && val.trim() === '') { + val = ''; + } + next[key] = val; }); valuesRef.current = next; + + const changedKeys = Object.keys(next).filter(k => next[k] !== prev[k]); + setBackendErrors(errPrev => { + let errNext = { ...errPrev }; + let modified = false; + for (const k of changedKeys) { + if (errNext[k]) { + delete errNext[k]; + modified = true; + } + } + return modified ? errNext : errPrev; + }); + return next; }); };