From b12cbe6741a1580a0bac5f4f71347ffea83a64a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20=C3=98sttveit?= <47412359+bjosttveit@users.noreply.github.com> Date: Wed, 23 Nov 2022 09:00:00 +0100 Subject: [PATCH] Give server rowIndex for validateRow trigger (#682) * Give server rowIndex for validateRow trigger * Always update group validations * send all rowIndices * use splitDashedKey instead --- .../layout/update/updateFormLayoutSagas.ts | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/altinn-app-frontend/src/features/form/layout/update/updateFormLayoutSagas.ts b/src/altinn-app-frontend/src/features/form/layout/update/updateFormLayoutSagas.ts index c922591197..6aa181fd6f 100644 --- a/src/altinn-app-frontend/src/features/form/layout/update/updateFormLayoutSagas.ts +++ b/src/altinn-app-frontend/src/features/form/layout/update/updateFormLayoutSagas.ts @@ -510,11 +510,12 @@ export function* updateRepeatingGroupEditIndexSaga({ payload: { group, index, validate }, }: PayloadAction): SagaIterator { try { - if (validate) { - const state: IRuntimeState = yield select(); + const state: IRuntimeState = yield select(); + const rowIndex = state.formLayout.uiConfig.repeatingGroups?.[group].editIndex; + + if (validate && typeof rowIndex === 'number' && rowIndex > -1) { const validations: IValidations = state.formValidations.validations; const currentView = state.formLayout.uiConfig.currentView; - const rowIndex = state.formLayout.uiConfig.repeatingGroups?.[group].editIndex; const frontendValidations: IValidations = validateGroup( group, @@ -522,9 +523,14 @@ export function* updateRepeatingGroupEditIndexSaga({ validate === Triggers.ValidateRow ? rowIndex : undefined, ); + // Get group's rowIndices to send to server for validations + const { depth: rowIndices } = splitDashedKey(group); + rowIndices.push(rowIndex); + const options: AxiosRequestConfig = { headers: { ComponentId: group, + RowIndex: rowIndices.join(','), }, }; @@ -562,25 +568,26 @@ export function* updateRepeatingGroupEditIndexSaga({ state.formLayout.layouts, state.textResources.resources, ); - const finalServerValidations: IValidations = filterValidationsByRow( - mappedServerValidations, - state.formLayout.layouts[currentView], - state.formLayout.uiConfig.repeatingGroups, - group, - validate === Triggers.ValidateRow ? rowIndex : undefined, - ); - const combinedValidations = mergeValidationObjects(frontendValidations, finalServerValidations); + const combinedValidations = mergeValidationObjects(frontendValidations, mappedServerValidations); + + // only overwrite validtions specific to the group - leave all other untouched + const newValidations = { + ...validations, + [currentView]: { + ...validations[currentView], + ...combinedValidations[currentView], + }, + }; + yield put(ValidationActions.updateValidations({ validations: newValidations })); const rowValidations = filterValidationsByRow( combinedValidations, state.formLayout.layouts[currentView], state.formLayout.uiConfig.repeatingGroups, group, - // Only compute if not already filtered - validate === Triggers.Validation ? rowIndex : undefined, + rowIndex, ); - if (canFormBeSaved({ validations: rowValidations, invalidDataTypes: false }, 'Complete')) { yield put( FormLayoutActions.updateRepeatingGroupsEditIndexFulfilled({ @@ -595,17 +602,6 @@ export function* updateRepeatingGroupEditIndexSaga({ }), ); } - if (!canFormBeSaved({ validations: combinedValidations, invalidDataTypes: false }, 'Complete')) { - // only overwrite validtions specific to the group - leave all other untouched - const newValidations = { - ...validations, - [currentView]: { - ...validations[currentView], - ...combinedValidations[currentView], - }, - }; - yield put(ValidationActions.updateValidations({ validations: newValidations })); - } } else { yield put( FormLayoutActions.updateRepeatingGroupsEditIndexFulfilled({