From 511550f996eea1eba8880d278d895b9dd96a3ac1 Mon Sep 17 00:00:00 2001 From: Wesley Barroso Lopes Date: Sun, 3 Mar 2024 13:36:06 -0300 Subject: [PATCH] Show validation error message as string instead of list (#5808) --- .../volto/locales/ca/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/de/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/en/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/es/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/eu/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/fi/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/fr/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/it/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/ja/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/nl/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/pt/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/pt_BR/LC_MESSAGES/volto.po | 6 +++++ .../volto/locales/ro/LC_MESSAGES/volto.po | 6 +++++ packages/volto/locales/volto.pot | 8 ++++++- .../volto/locales/zh_CN/LC_MESSAGES/volto.po | 6 +++++ packages/volto/news/1868.bugfix | 1 + .../volto/src/components/manage/Add/Add.jsx | 24 ++++++++++++++++++- .../volto/src/components/manage/Edit/Edit.jsx | 24 ++++++++++++++++++- .../helpers/FormValidation/FormValidation.js | 2 +- packages/volto/src/helpers/index.js | 1 + 20 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 packages/volto/news/1868.bugfix diff --git a/packages/volto/locales/ca/LC_MESSAGES/volto.po b/packages/volto/locales/ca/LC_MESSAGES/volto.po index 124ebbcd01..517af40068 100644 --- a/packages/volto/locales/ca/LC_MESSAGES/volto.po +++ b/packages/volto/locales/ca/LC_MESSAGES/volto.po @@ -3656,6 +3656,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/de/LC_MESSAGES/volto.po b/packages/volto/locales/de/LC_MESSAGES/volto.po index 6011f4fed2..daebb01e75 100644 --- a/packages/volto/locales/de/LC_MESSAGES/volto.po +++ b/packages/volto/locales/de/LC_MESSAGES/volto.po @@ -3655,6 +3655,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/en/LC_MESSAGES/volto.po b/packages/volto/locales/en/LC_MESSAGES/volto.po index 19b258fecb..d54686ff84 100644 --- a/packages/volto/locales/en/LC_MESSAGES/volto.po +++ b/packages/volto/locales/en/LC_MESSAGES/volto.po @@ -3650,6 +3650,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/es/LC_MESSAGES/volto.po b/packages/volto/locales/es/LC_MESSAGES/volto.po index f9f065a686..0a9b7f60cd 100644 --- a/packages/volto/locales/es/LC_MESSAGES/volto.po +++ b/packages/volto/locales/es/LC_MESSAGES/volto.po @@ -3657,6 +3657,12 @@ msgstr "No hay grupos con los criterios buscados." msgid "There are no users with the searched criteria" msgstr "No hay usuarios con los criterios buscados" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/eu/LC_MESSAGES/volto.po b/packages/volto/locales/eu/LC_MESSAGES/volto.po index 7064a7b56c..2afcc9f192 100644 --- a/packages/volto/locales/eu/LC_MESSAGES/volto.po +++ b/packages/volto/locales/eu/LC_MESSAGES/volto.po @@ -3657,6 +3657,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/fi/LC_MESSAGES/volto.po b/packages/volto/locales/fi/LC_MESSAGES/volto.po index 03b2780a8c..ff5107a887 100644 --- a/packages/volto/locales/fi/LC_MESSAGES/volto.po +++ b/packages/volto/locales/fi/LC_MESSAGES/volto.po @@ -3655,6 +3655,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/fr/LC_MESSAGES/volto.po b/packages/volto/locales/fr/LC_MESSAGES/volto.po index 79c1ee9372..fd79824475 100644 --- a/packages/volto/locales/fr/LC_MESSAGES/volto.po +++ b/packages/volto/locales/fr/LC_MESSAGES/volto.po @@ -3657,6 +3657,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/it/LC_MESSAGES/volto.po b/packages/volto/locales/it/LC_MESSAGES/volto.po index ee27a0e94c..6353bffafb 100644 --- a/packages/volto/locales/it/LC_MESSAGES/volto.po +++ b/packages/volto/locales/it/LC_MESSAGES/volto.po @@ -3650,6 +3650,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/ja/LC_MESSAGES/volto.po b/packages/volto/locales/ja/LC_MESSAGES/volto.po index 23c0849a3c..200948bd4a 100644 --- a/packages/volto/locales/ja/LC_MESSAGES/volto.po +++ b/packages/volto/locales/ja/LC_MESSAGES/volto.po @@ -3655,6 +3655,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/nl/LC_MESSAGES/volto.po b/packages/volto/locales/nl/LC_MESSAGES/volto.po index fe21ea8d91..e5bb5478f5 100644 --- a/packages/volto/locales/nl/LC_MESSAGES/volto.po +++ b/packages/volto/locales/nl/LC_MESSAGES/volto.po @@ -3654,6 +3654,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/pt/LC_MESSAGES/volto.po b/packages/volto/locales/pt/LC_MESSAGES/volto.po index c341c5f237..2239501dc2 100644 --- a/packages/volto/locales/pt/LC_MESSAGES/volto.po +++ b/packages/volto/locales/pt/LC_MESSAGES/volto.po @@ -3655,6 +3655,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/pt_BR/LC_MESSAGES/volto.po b/packages/volto/locales/pt_BR/LC_MESSAGES/volto.po index 663e5e5ab4..fb355baa90 100644 --- a/packages/volto/locales/pt_BR/LC_MESSAGES/volto.po +++ b/packages/volto/locales/pt_BR/LC_MESSAGES/volto.po @@ -3656,6 +3656,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "Existem alguns erros." + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/ro/LC_MESSAGES/volto.po b/packages/volto/locales/ro/LC_MESSAGES/volto.po index 2c75e1aa99..0f5512ad88 100644 --- a/packages/volto/locales/ro/LC_MESSAGES/volto.po +++ b/packages/volto/locales/ro/LC_MESSAGES/volto.po @@ -3650,6 +3650,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/volto.pot b/packages/volto/locales/volto.pot index 414d63d872..2791c609c7 100644 --- a/packages/volto/locales/volto.pot +++ b/packages/volto/locales/volto.pot @@ -1,7 +1,7 @@ msgid "" msgstr "" "Project-Id-Version: Plone\n" -"POT-Creation-Date: 2024-02-26T09:23:35.781Z\n" +"POT-Creation-Date: 2024-02-26T19:56:41.001Z\n" "Last-Translator: Plone i18n \n" "Language-Team: Plone i18n \n" "Content-Type: text/plain; charset=utf-8\n" @@ -3652,6 +3652,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/locales/zh_CN/LC_MESSAGES/volto.po b/packages/volto/locales/zh_CN/LC_MESSAGES/volto.po index 105ead2efc..eb6b8a7ade 100644 --- a/packages/volto/locales/zh_CN/LC_MESSAGES/volto.po +++ b/packages/volto/locales/zh_CN/LC_MESSAGES/volto.po @@ -3656,6 +3656,12 @@ msgstr "" msgid "There are no users with the searched criteria" msgstr "" +#. Default: "There are some errors." +#: components/manage/Add/Add +#: components/manage/Edit/Edit +msgid "There are some errors." +msgstr "" + #. Default: "There is a configuration problem on the backend" #: components/theme/CorsError/CorsError msgid "There is a configuration problem on the backend" diff --git a/packages/volto/news/1868.bugfix b/packages/volto/news/1868.bugfix new file mode 100644 index 0000000000..f807b6ba1f --- /dev/null +++ b/packages/volto/news/1868.bugfix @@ -0,0 +1 @@ +Show validation error message as string instead of list. @wesleybl diff --git a/packages/volto/src/components/manage/Add/Add.jsx b/packages/volto/src/components/manage/Add/Add.jsx index c16b41e2d7..f5f187d9c5 100644 --- a/packages/volto/src/components/manage/Add/Add.jsx +++ b/packages/volto/src/components/manage/Add/Add.jsx @@ -39,6 +39,7 @@ import { } from '@plone/volto/helpers'; import { preloadLazyLibs } from '@plone/volto/helpers/Loadable'; +import { tryParseJSON } from '@plone/volto/helpers'; import config from '@plone/volto/registry'; @@ -66,6 +67,10 @@ const messages = defineMessages({ id: 'Translate to {lang}', defaultMessage: 'Translate to {lang}', }, + someErrors: { + id: 'There are some errors.', + defaultMessage: 'There are some errors.', + }, }); /** @@ -168,13 +173,30 @@ class Add extends Component { new DOMParser().parseFromString(message, 'text/html')?.all[0] ?.textContent || message; + const errorsList = tryParseJSON(error); + let erroMessage; + if (Array.isArray(errorsList)) { + const invariantErrors = errorsList + .filter((errorItem) => !('field' in errorItem)) + .map((errorItem) => errorItem['message']); + if (invariantErrors.length > 0) { + // Plone invariant validation message. + erroMessage = invariantErrors.join(' - '); + } else { + // Error in specific field. + erroMessage = this.props.intl.formatMessage(messages.someErrors); + } + } else { + erroMessage = error; + } + this.setState({ error: error }); toast.error( , ); } diff --git a/packages/volto/src/components/manage/Edit/Edit.jsx b/packages/volto/src/components/manage/Edit/Edit.jsx index 54f1fd1b48..56e40f7996 100644 --- a/packages/volto/src/components/manage/Edit/Edit.jsx +++ b/packages/volto/src/components/manage/Edit/Edit.jsx @@ -41,6 +41,7 @@ import { hasBlocksData, } from '@plone/volto/helpers'; import { preloadLazyLibs } from '@plone/volto/helpers/Loadable'; +import { tryParseJSON } from '@plone/volto/helpers'; import saveSVG from '@plone/volto/icons/save.svg'; import clearSVG from '@plone/volto/icons/clear.svg'; @@ -64,6 +65,10 @@ const messages = defineMessages({ id: 'Error', defaultMessage: 'Error', }, + someErrors: { + id: 'There are some errors.', + defaultMessage: 'There are some errors.', + }, }); /** @@ -198,13 +203,30 @@ class Edit extends Component { new DOMParser().parseFromString(message, 'text/html')?.all[0] ?.textContent || message; + const errorsList = tryParseJSON(error); + let erroMessage; + if (Array.isArray(errorsList)) { + const invariantErrors = errorsList + .filter((errorItem) => !('field' in errorItem)) + .map((errorItem) => errorItem['message']); + if (invariantErrors.length > 0) { + // Plone invariant validation message. + erroMessage = invariantErrors.join(' - '); + } else { + // Error in specific field. + erroMessage = this.props.intl.formatMessage(messages.someErrors); + } + } else { + erroMessage = error; + } + this.setState({ error: error }); toast.error( , ); } diff --git a/packages/volto/src/helpers/FormValidation/FormValidation.js b/packages/volto/src/helpers/FormValidation/FormValidation.js index df276bf11a..95c470fa77 100644 --- a/packages/volto/src/helpers/FormValidation/FormValidation.js +++ b/packages/volto/src/helpers/FormValidation/FormValidation.js @@ -157,7 +157,7 @@ const widgetValidation = { * The string that comes my not be a valid JSON * @param {string} requestItem */ -const tryParseJSON = (requestItem) => { +export const tryParseJSON = (requestItem) => { let resultObj = null; try { resultObj = JSON.parse(requestItem); diff --git a/packages/volto/src/helpers/index.js b/packages/volto/src/helpers/index.js index 99cf5aa21e..dd5d5f803a 100644 --- a/packages/volto/src/helpers/index.js +++ b/packages/volto/src/helpers/index.js @@ -81,6 +81,7 @@ export { default as langmap } from './LanguageMap/LanguageMap'; export { default as Helmet } from './Helmet/Helmet'; export { default as FormValidation } from './FormValidation/FormValidation'; export { validateFileUploadSize } from './FormValidation/FormValidation'; +export { tryParseJSON } from './FormValidation/FormValidation'; export { difference, getColor,