Skip to content

Commit

Permalink
Show validation error message as string instead of list (#5842)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl authored Mar 19, 2024
1 parent 78f8a75 commit bf471c1
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 4 deletions.
6 changes: 6 additions & 0 deletions locales/ca/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,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 locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,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 locales/en/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,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 locales/es/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,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 locales/eu/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 locales/fi/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,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 locales/fr/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 locales/it/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,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 locales/ja/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,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 locales/nl/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3647,6 +3647,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 locales/pt/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,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 locales/pt_BR/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,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 locales/ro/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,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 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-02T15:43:45.656Z\n"
"POT-Creation-Date: 2024-03-05T13:17:03.192Z\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 @@ -3645,6 +3645,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 locales/zh_CN/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -3649,6 +3649,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 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 src/components/manage/Add/Add.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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 @@ -71,6 +72,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 @@ -174,13 +179,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 src/components/manage/Edit/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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 @@ -65,6 +66,10 @@ const messages = defineMessages({
id: 'Error',
defaultMessage: 'Error',
},
someErrors: {
id: 'There are some errors.',
defaultMessage: 'There are some errors.',
},
});

/**
Expand Down Expand Up @@ -200,13 +205,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
2 changes: 1 addition & 1 deletion src/helpers/FormValidation/FormValidation.js
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 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 bf471c1

Please sign in to comment.