From 131b2dbf57488a533664980a52e3ec2a95740c0f Mon Sep 17 00:00:00 2001 From: Vitalii Kiiko Date: Sun, 13 Oct 2024 12:41:40 +0200 Subject: [PATCH] - added table validation custom function; --- src/helpers/validators.js | 31 +++++++++++++++---- src/pages/BandoApplication/index.js | 1 + .../BuilderElementSettings/index.js | 1 - src/pages/DashboardBeneficiario/index.js | 4 +-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/helpers/validators.js b/src/helpers/validators.js index 3ccc929..e4f9aff 100644 --- a/src/helpers/validators.js +++ b/src/helpers/validators.js @@ -1,5 +1,5 @@ import validate from 'validate.js'; -import { match, isEmpty, is } from 'ramda'; +import { match, isEmpty, is, pathOr, isNil } from 'ramda'; import CodiceFiscale from 'codice-fiscale-js'; export const isPIVA = (v) => { @@ -67,12 +67,31 @@ export const maxChecks = (v, num) => { return is(Array, v) ? v.length <= parseInt(num) : false; } -export const nonEmptyTables = (v = [], colsCfg = []) => { +export const nonEmptyTables = (v = [], tableCfg = []) => { //console.log('nonEmptyTables',v) - /*const cellValues = v.map(row => { + const colsCfg = pathOr([], ['stateFieldData'], tableCfg); + const nonPredefinedCells = colsCfg + .filter(o => !o.predefined) + .map(o => o.name); + let isTableValid = true; + let atLeastOneCellFilled = false - return isEmpty(row) + // eslint-disable-next-line array-callback-return + v.map((row) => { + if (isEmpty(row)) { + isTableValid = false; + } else { + // eslint-disable-next-line array-callback-return + nonPredefinedCells.map((k) => { + if (isNil(row[k]) || isEmpty(row[k])) { + isTableValid = atLeastOneCellFilled; + } else { + atLeastOneCellFilled = true; + isTableValid = true; + } + }); + } }); - console.log('cellValues', cellValues, colsCfg)*/ - return is(Array, v) ? v.length >= 1 : false; + + return is(Array, v) ? v.length >= 1 && isTableValid : false; } \ No newline at end of file diff --git a/src/pages/BandoApplication/index.js b/src/pages/BandoApplication/index.js index 80b58cc..e6c3150 100644 --- a/src/pages/BandoApplication/index.js +++ b/src/pages/BandoApplication/index.js @@ -156,6 +156,7 @@ const BandoApplication = () => { const saveDraft = (saveAndMove = '') => { trigger(); + const formValues = getValues(); const usedFieldsIds = pluck('id', formData); const newFormValues = Object.keys(formValues) diff --git a/src/pages/BandoFormsEdit/components/BuilderElementSettings/index.js b/src/pages/BandoFormsEdit/components/BuilderElementSettings/index.js index 7be02dc..9ade547 100644 --- a/src/pages/BandoFormsEdit/components/BuilderElementSettings/index.js +++ b/src/pages/BandoFormsEdit/components/BuilderElementSettings/index.js @@ -14,7 +14,6 @@ import { TabView, TabPanel } from 'primereact/tabview'; import { InputSwitch } from 'primereact/inputswitch'; import ElementSetting from './components/ElementSetting'; import { Dropdown } from 'primereact/dropdown'; -import { maxChecks, minChecks } from '../../../../helpers/validators'; const BuilderElementSettings = ({ closeSettings }) => { const elements = useStore().main.formElements(); diff --git a/src/pages/DashboardBeneficiario/index.js b/src/pages/DashboardBeneficiario/index.js index af1a9ac..917e3e6 100644 --- a/src/pages/DashboardBeneficiario/index.js +++ b/src/pages/DashboardBeneficiario/index.js @@ -65,7 +65,7 @@ const DashboardBeneficiario = () => {