Skip to content

Commit

Permalink
fix: add pluggable custom validation for all datagrid fields, not jus…
Browse files Browse the repository at this point in the history
…t required ones and applied new validation to timeline_tempi_scadenze (#357)

* fix: add pluggable custom validation for all datagrid fields, not just required ones and applied new validation to timeline_tempi_scadenze

* fix: validation check for timeline_tempi_scadenze

* fix: customizations doc comment example
  • Loading branch information
deodorhunter authored Oct 16, 2023
1 parent b3ec883 commit 9c6f55c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 3 deletions.
5 changes: 5 additions & 0 deletions locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3748,6 +3748,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr ""

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
5 changes: 5 additions & 0 deletions locales/en/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr "Insert a value"

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
5 changes: 5 additions & 0 deletions locales/es/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3742,6 +3742,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr "Insertar un valor"

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
5 changes: 5 additions & 0 deletions locales/fr/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3750,6 +3750,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr "Entrez du texte"

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
5 changes: 5 additions & 0 deletions locales/it/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr "Inserisci un valore"

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
7 changes: 6 additions & 1 deletion locales/volto.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: Plone\n"
"POT-Creation-Date: 2023-09-20T11:30:56.175Z\n"
"POT-Creation-Date: 2023-10-10T15:52:06.253Z\n"
"Last-Translator: Plone i18n <[email protected]>\n"
"Language-Team: Plone i18n <[email protected]>\n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -3735,6 +3735,11 @@ msgstr ""
msgid "text_filter_placeholder"
msgstr ""

#: helpers/FormValidation/DataGridFormValidationHelpers
# defaultMessage: Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"
msgid "timeline_tempi_scadenze_validation_error"
msgstr ""

#: components/ItaliaTheme/View/BandoView/BandoText
# defaultMessage: Tipologia del bando
msgid "tipologia_bando"
Expand Down
23 changes: 21 additions & 2 deletions src/customizations/volto/helpers/FormValidation/FormValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
* - gestione motivo dello stato di servizio che è required solo se il servizio non è fruibile
* - gestione campi tipo dataGridField
* - gestione timeline_tempi_scadenze nel servizio che ha solo un campo richiesto su 5
* - aggiunta la possibilita' di avere validazioni pluggable per ogni campo con widget data_grid,
* aggiungi i tuoi dgfield e le funzioni di validazione sotto CUSTOM_DGFIELD_VALIDATION.
* Rispettare la convenzione con l'id del campo proveniente dallo schema del CT, ie:
* export const CUSTOM_DGFIELD_VALIDATION = {
timeline_tempi_scadenze: {
isValid: (value, itemObj, intlFunc) => {
const isValid = value.every((val, i) => val.title);
return !isValid
? intlFunc(
dgfieldValidationMessages.timeline_tempi_scadenze_validation_error,
)
: null;
},
},
};
*/
import { map, uniq, keys, intersection, isEmpty } from 'lodash';
import { messages } from '@plone/volto/helpers/MessageLabels/MessageLabels';
Expand All @@ -13,6 +28,9 @@ import {
serviceFormValidationHelper,
eventFormValidationHelper,
getRealEmptyField,
getSpecificDataGridFieldValidation,
realWidgetType,
CUSTOM_DGFIELD_VALIDATION,
} from 'design-comuni-plone-theme/helpers';
import config from '@plone/volto/registry';

Expand Down Expand Up @@ -162,6 +180,7 @@ const widgetValidation = {
maximum: (value, itemObj, intlFunc) =>
isMaxPropertyValid(value, itemObj.maximum, 'maximum', intlFunc),
},
...CUSTOM_DGFIELD_VALIDATION,
};

/**
Expand Down Expand Up @@ -288,10 +307,10 @@ const validateFieldsPerFieldset = (
);

map(schema.properties, (field, fieldId) => {
const fieldWidgetType = field.widget || field.type;
const fieldWidgetType = realWidgetType(field, fieldId);
const widgetValidationCriteria = widgetValidation[fieldWidgetType]
? Object.keys(widgetValidation[fieldWidgetType])
: [];
: getSpecificDataGridFieldValidation(fieldId);
let fieldData = formData[fieldId];
// test each criterion ex maximum, isEmail, isUrl, maxLength etc
const fieldErrors = widgetValidationCriteria
Expand Down
38 changes: 38 additions & 0 deletions src/helpers/FormValidation/DataGridFormValidationHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { defineMessages } from 'react-intl';

const dgfieldValidationMessages = defineMessages({
timeline_tempi_scadenze_validation_error: {
id: 'timeline_tempi_scadenze_validation_error',
defaultMessage:
'Impossibile aggiungere un elemento alla timeline senza aver compilato il campo "Titolo"',
},
});

export const CUSTOM_DGFIELD_VALIDATION = {
timeline_tempi_scadenze: {
isValid: (value, itemObj, intlFunc) => {
const isValid = value.every((val, i) => val.title);
return !isValid
? intlFunc(
dgfieldValidationMessages.timeline_tempi_scadenze_validation_error,
)
: null;
},
},
};

// Obbligati a far cosi' perche' il backend non è abbastanza furbo da definire
// una specifica factory non innestata o field.widget ofield.type per dgf e
// invece fornisce solo un generico "List" o "array", che avrebbe
// un'altra validazione in Volto.
export const realWidgetType = (field, fieldId) => {
return field?.widgetOptions?.frontendOptions?.widget === 'data_grid'
? fieldId
: field.widget || field.type;
};

export const getSpecificDataGridFieldValidation = (fieldId) => {
return fieldId && CUSTOM_DGFIELD_VALIDATION.hasOwnProperty(fieldId)
? Object.keys(CUSTOM_DGFIELD_VALIDATION[fieldId])
: [];
};
5 changes: 5 additions & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ export {
getRealEmptyField,
eventFormValidationHelper,
} from 'design-comuni-plone-theme/helpers/FormValidation/FormValidationHelpers';
export {
getSpecificDataGridFieldValidation,
realWidgetType,
CUSTOM_DGFIELD_VALIDATION,
} from 'design-comuni-plone-theme/helpers/FormValidation/DataGridFormValidationHelpers';

export { commonSearchBlockMessages } from 'design-comuni-plone-theme/helpers/Translations/searchBlockExtendedTranslations';

0 comments on commit 9c6f55c

Please sign in to comment.