From 3f482fdecc93647d71bc8410a022b8d7a4ea7047 Mon Sep 17 00:00:00 2001 From: faiztyanirh Date: Wed, 1 Apr 2026 10:35:31 +0700 Subject: [PATCH] fix rule val factor to config --- .../test/config/test-form-config.js | 118 ++++++++++-------- .../dictionary/test/page/create-page.svelte | 14 +-- .../dictionary/test/page/edit-page.svelte | 8 ++ .../form/dictionary-form-renderer.svelte | 4 + 4 files changed, 84 insertions(+), 60 deletions(-) diff --git a/src/lib/components/dictionary/test/config/test-form-config.js b/src/lib/components/dictionary/test/config/test-form-config.js index 9af476b..1cc3522 100644 --- a/src/lib/components/dictionary/test/config/test-form-config.js +++ b/src/lib/components/dictionary/test/config/test-form-config.js @@ -8,8 +8,8 @@ export const testSchema = z .object({ TestSiteCode: z.string().min(1, 'Required'), TestSiteName: z.string().min(1, 'Required'), - Factor: z.string().optional(), - Unit2: z.string().optional(), + Factor: z.preprocess((val) => (val === '' || val === null || val === undefined ? undefined : Number(val)), z.number().optional()), + Unit2: z.preprocess((val) => (val === null ? undefined : val), z.string().optional()), Decimal: z.preprocess( (val) => { if (val === '' || val === null || val === undefined) return undefined; @@ -79,37 +79,37 @@ export const testGroupSchema = z .array( z.object({ id: z.number(), - value: z.string(), - }) - ) - .optional() + value: z.string() + }) + ) + .optional() }) .superRefine((data, ctx) => { if (!data.Members) return; - const values = data.Members.map(m => m.value).filter(Boolean); + const values = data.Members.map((m) => m.value).filter(Boolean); const duplicates = values.filter((v, i) => values.indexOf(v) !== i); if (duplicates.length) { - const uniqueDuplicates = [...new Set(duplicates)]; + const uniqueDuplicates = [...new Set(duplicates)]; - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: `Duplicate member : ${uniqueDuplicates.join(", ")}`, - path: ["Members"] - }); + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Duplicate member : ${uniqueDuplicates.join(', ')}`, + path: ['Members'] + }); } }); export const refNumSchema = z .object({ - AgeStart: z.coerce.string().optional(), - AgeEnd: z.coerce.string().optional(), - Low: z.coerce.string().optional(), - High: z.coerce.string().optional(), - LowSign: z.string().optional(), - HighSign: z.string().optional(), - NumRefType: z.string().optional() + AgeStart: z.coerce.string().optional(), + AgeEnd: z.coerce.string().optional(), + Low: z.coerce.string().optional(), + High: z.coerce.string().optional(), + LowSign: z.string().optional(), + HighSign: z.string().optional(), + NumRefType: z.string().optional() }) .superRefine((data, ctx) => { const start = toDays(data.AgeStart); @@ -150,7 +150,7 @@ export const refNumSchema = z ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Low sign must be =, > or >=', - path: ['LowSign'], + path: ['LowSign'] }); } if (data.High && !data.HighSign) { @@ -164,7 +164,7 @@ export const refNumSchema = z ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'High sign must be =, < or <=', - path: ['HighSign'], + path: ['HighSign'] }); } if (data.LowSign && data.HighSign && data.LowSign === data.HighSign) { @@ -245,7 +245,7 @@ export const testInitialForm = { isVisibleRpt: 1, isCountStat: 1, Level: '', - isRequestable: 1, + isRequestable: 1 }; export const testCalInitialForm = { @@ -258,8 +258,8 @@ export const testCalInitialForm = { export const testGroupInitialForm = { TestGrpID: '', TestSiteID: '', - Members: [], -} + Members: [] +}; export const refNumInitialForm = { RefNumID: '', @@ -319,12 +319,12 @@ export const testDefaultErrors = { export const testCalDefaultErrors = { FormulaInput: null, - FormulaCode: null, + FormulaCode: null }; export const testGroupDefaultErrors = { - Members: null, -} + Members: null +}; export const refNumDefaultErrors = { AgeStart: null, @@ -479,7 +479,9 @@ export const testFormFields = [ key: 'Factor', label: 'Factor', required: false, - type: 'number' + type: 'number', + validateOn: ['input'], + triggerFields: ['Unit2'] } ] }, @@ -490,7 +492,8 @@ export const testFormFields = [ key: 'Unit2', label: 'Unit 2', required: false, - type: 'text' + type: 'text', + validateOn: ['input'] }, { key: 'Decimal', @@ -571,7 +574,7 @@ export const testFormFields = [ label: 'Sequence on Report', required: false, type: 'text' - }, + } ] }, { @@ -584,8 +587,8 @@ export const testFormFields = [ type: 'toggle', optionsToggle: [ { value: 0, label: 'Disabled' }, - { value: 1, label: 'Enabled' }, - ], + { value: 1, label: 'Enabled' } + ] }, { key: 'isVisibleRpt', @@ -594,8 +597,8 @@ export const testFormFields = [ type: 'toggle', optionsToggle: [ { value: 0, label: 'Disabled' }, - { value: 1, label: 'Enabled' }, - ], + { value: 1, label: 'Enabled' } + ] } ] }, @@ -609,8 +612,8 @@ export const testFormFields = [ type: 'toggle', optionsToggle: [ { value: 0, label: 'Disabled' }, - { value: 1, label: 'Enabled' }, - ], + { value: 1, label: 'Enabled' } + ] }, { key: 'isRequestable', @@ -619,14 +622,14 @@ export const testFormFields = [ type: 'toggle', optionsToggle: [ { value: 0, label: 'Disabled' }, - { value: 1, label: 'Enabled' }, + { value: 1, label: 'Enabled' } ], fullWidth: false - }, + } ] - }, + } ] - }, + } ]; export const testCalFormFields = [ @@ -679,7 +682,7 @@ export const testGroupFormFields = [ validateOn: ['input'] } ] - }, + } ] } ]; @@ -764,7 +767,7 @@ export const refNumFormFields = [ label: 'Range Type', required: false, type: 'select', - optionsEndpoint: `${API.BASE_URL}${API.VALUESET}/range_type`, + optionsEndpoint: `${API.BASE_URL}${API.VALUESET}/range_type` } ] }, @@ -1049,7 +1052,14 @@ export function getTestFormActions(handlers) { ]; } -export function buildTestPayload({ mainForm, activeFormStates, testType, refNumData, refTxtData, mapData }) { +export function buildTestPayload({ + mainForm, + activeFormStates, + testType, + refNumData, + refTxtData, + mapData +}) { let payload = { ...mainForm }; @@ -1059,22 +1069,24 @@ export function buildTestPayload({ mainForm, activeFormStates, testType, refNumD if (key === 'refNum' && refNumData?.length > 0) { // payload[key] = refNumData; - payload.refnum = refNumData.map(row => ({ + payload.refnum = refNumData.map((row) => ({ ...row, AgeStart: toDays(row.AgeStart), - AgeEnd: toDays(row.AgeEnd), - })) + AgeEnd: toDays(row.AgeEnd) + })); } else if (key === 'refTxt' && refTxtData?.length > 0) { // payload[key] = refTxtData; - payload.reftxt = refTxtData.map(row => ({ + payload.reftxt = refTxtData.map((row) => ({ ...row, AgeStart: toDays(row.AgeStart), - AgeEnd: toDays(row.AgeEnd), - })) - } else if(key === 'group' && state.form?.Members?.length > 0) { + AgeEnd: toDays(row.AgeEnd) + })); + } else if (key === 'group' && state.form?.Members?.length > 0) { payload.testdefgrp = { ...state.form, - Members: state.form?.Members?.filter((m) => m.value).map((m) => ({ TestSiteID: Number(m.value) })) + Members: state.form?.Members?.filter((m) => m.value).map((m) => ({ + TestSiteID: Number(m.value) + })) }; } else if (key === 'map' && mapData?.length > 0) { payload.testmap = mapData; @@ -1082,7 +1094,7 @@ export function buildTestPayload({ mainForm, activeFormStates, testType, refNumD payload.testdefcal = { ...state.form, FormulaInput: state.form?.FormulaInput?.map((m) => ({ TestSiteID: Number(m.testid) })) - } + }; } // else if ((key === 'cal' || key === 'map') && state?.form) { // payload[key] = { diff --git a/src/lib/components/dictionary/test/page/create-page.svelte b/src/lib/components/dictionary/test/page/create-page.svelte index b1e4b6e..abeeafa 100644 --- a/src/lib/components/dictionary/test/page/create-page.svelte +++ b/src/lib/components/dictionary/test/page/create-page.svelte @@ -347,13 +347,13 @@ } }); - $effect(() => { - if (formState.form.Factor && !formState.form.Unit2) { - formState.errors.Unit2 = 'Required'; - } else { - formState.errors.Unit2 = null; - } - }); + // $effect(() => { + // if (formState.form.Factor && !formState.form.Unit2) { + // formState.errors.Unit2 = 'Required'; + // } else { + // formState.errors.Unit2 = null; + // } + // }); $effect(() => { const allColumns = formFields.flatMap((section) => diff --git a/src/lib/components/dictionary/test/page/edit-page.svelte b/src/lib/components/dictionary/test/page/edit-page.svelte index c4a5833..4e42607 100644 --- a/src/lib/components/dictionary/test/page/edit-page.svelte +++ b/src/lib/components/dictionary/test/page/edit-page.svelte @@ -450,6 +450,14 @@ refTxtState.form.TxtRefType = value; } } + + // $effect(() => { + // if (formState.form.Factor && !formState.form.Unit2) { + // formState.errors.Unit2 = 'Required'; + // } else { + // formState.errors.Unit2 = null; + // } + // }); $effect(() => { const refType = formState.form.RefType; diff --git a/src/lib/components/reusable/form/dictionary-form-renderer.svelte b/src/lib/components/reusable/form/dictionary-form-renderer.svelte index dc60dec..9676a01 100644 --- a/src/lib/components/reusable/form/dictionary-form-renderer.svelte +++ b/src/lib/components/reusable/form/dictionary-form-renderer.svelte @@ -118,6 +118,7 @@ options, optionsToggle, validateOn, + triggerFields, dependsOn, endpointParamKey, valueKey, @@ -186,6 +187,9 @@ oninput={() => { if (validateOn?.includes('input')) { formState.validateField(key, formState.form[key], false); + for (const related of (triggerFields ?? [])) { + formState.validateField(related, formState.form[related], false); + } } }} onblur={() => {