Skip to content

Commit

Permalink
Show validation error message as string instead of list (#5808)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl authored Mar 3, 2024
1 parent 65ec519 commit 511550f
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/volto/locales/ca/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/en/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/es/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/eu/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/fi/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/fr/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/it/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/ja/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/nl/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/pt/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/pt_BR/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/ro/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion packages/volto/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: 2024-02-26T09:23:35.781Z\n"
"POT-Creation-Date: 2024-02-26T19:56:41.001Z\n"
"Last-Translator: Plone i18n <[email protected]>\n"
"Language-Team: Plone i18n <[email protected]>\n"
"Content-Type: text/plain; charset=utf-8\n"
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/volto/locales/zh_CN/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions packages/volto/news/1868.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show validation error message as string instead of list. @wesleybl
24 changes: 23 additions & 1 deletion packages/volto/src/components/manage/Add/Add.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.',
},
});

/**
Expand Down Expand Up @@ -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(
<Toast
error
title={this.props.intl.formatMessage(messages.error)}
content={`${nextProps.createRequest.error.status}: ${error}`}
content={erroMessage}
/>,
);
}
Expand Down
24 changes: 23 additions & 1 deletion packages/volto/src/components/manage/Edit/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -64,6 +65,10 @@ const messages = defineMessages({
id: 'Error',
defaultMessage: 'Error',
},
someErrors: {
id: 'There are some errors.',
defaultMessage: 'There are some errors.',
},
});

/**
Expand Down Expand Up @@ -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(
<Toast
error
title={this.props.intl.formatMessage(messages.error)}
content={`${nextProps.updateRequest.error.status} ${error}`}
content={erroMessage}
/>,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/volto/src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 511550f

Please sign in to comment.