diff --git a/packages/volto/src/components/manage/Add/Add.jsx b/packages/volto/src/components/manage/Add/Add.jsx index 0510b43f5a1..4e07e4c5a3a 100644 --- a/packages/volto/src/components/manage/Add/Add.jsx +++ b/packages/volto/src/components/manage/Add/Add.jsx @@ -5,7 +5,11 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { BodyClass, Helmet } from '@plone/volto/helpers'; +import { + BodyClass, + Helmet, + extractInvariantErrors, +} from '@plone/volto/helpers'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { keys, isEmpty } from 'lodash'; @@ -182,9 +186,7 @@ class Add extends Component { const errorsList = tryParseJSON(error); let erroMessage; if (Array.isArray(errorsList)) { - const invariantErrors = errorsList - .filter((errorItem) => !('field' in errorItem)) - .map((errorItem) => errorItem['message']); + const invariantErrors = extractInvariantErrors(errorsList); if (invariantErrors.length > 0) { // Plone invariant validation message. erroMessage = invariantErrors.join(' - '); diff --git a/packages/volto/src/components/manage/Controlpanels/Controlpanel.jsx b/packages/volto/src/components/manage/Controlpanels/Controlpanel.jsx index dee0e5e221b..9970c962b59 100644 --- a/packages/volto/src/components/manage/Controlpanels/Controlpanel.jsx +++ b/packages/volto/src/components/manage/Controlpanels/Controlpanel.jsx @@ -8,7 +8,11 @@ import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { withRouter } from 'react-router-dom'; -import { Helmet, tryParseJSON } from '@plone/volto/helpers'; +import { + Helmet, + tryParseJSON, + extractInvariantErrors, +} from '@plone/volto/helpers'; import { createPortal } from 'react-dom'; import { Button, Container } from 'semantic-ui-react'; import { defineMessages, injectIntl } from 'react-intl'; @@ -131,9 +135,7 @@ class Controlpanel extends Component { const errorsList = tryParseJSON(error); let invariantErrors = []; if (Array.isArray(errorsList)) { - invariantErrors = errorsList - .filter((errorItem) => !('field' in errorItem)) - .map((errorItem) => errorItem['message']); + invariantErrors = extractInvariantErrors(errorsList); } this.setState({ error: error }); diff --git a/packages/volto/src/components/manage/Edit/Edit.jsx b/packages/volto/src/components/manage/Edit/Edit.jsx index 7be1fa906be..333f6cf6514 100644 --- a/packages/volto/src/components/manage/Edit/Edit.jsx +++ b/packages/volto/src/components/manage/Edit/Edit.jsx @@ -5,7 +5,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { Helmet } from '@plone/volto/helpers'; +import { Helmet, extractInvariantErrors } from '@plone/volto/helpers'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { asyncConnect, hasApiExpander } from '@plone/volto/helpers'; @@ -208,9 +208,7 @@ class Edit extends Component { const errorsList = tryParseJSON(error); let erroMessage; if (Array.isArray(errorsList)) { - const invariantErrors = errorsList - .filter((errorItem) => !('field' in errorItem)) - .map((errorItem) => errorItem['message']); + const invariantErrors = extractInvariantErrors(errorsList); if (invariantErrors.length > 0) { // Plone invariant validation message. erroMessage = invariantErrors.join(' - '); diff --git a/packages/volto/src/helpers/FormValidation/FormValidation.js b/packages/volto/src/helpers/FormValidation/FormValidation.js index c9bba4210e4..d575d596397 100644 --- a/packages/volto/src/helpers/FormValidation/FormValidation.js +++ b/packages/volto/src/helpers/FormValidation/FormValidation.js @@ -406,3 +406,13 @@ export const validateFileUploadSize = (file, intlFunc) => { } return isValid; }; + +/** + * Extract invariant errors given an array of errors. + * @param {Array} erros + */ +export const extractInvariantErrors = (erros) => { + return erros + .filter((errorItem) => !('field' in errorItem)) + .map((errorItem) => errorItem['message']); +}; diff --git a/packages/volto/src/helpers/index.js b/packages/volto/src/helpers/index.js index efa3d4b9fd5..20f50538ea6 100644 --- a/packages/volto/src/helpers/index.js +++ b/packages/volto/src/helpers/index.js @@ -82,6 +82,7 @@ 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 { extractInvariantErrors } from './FormValidation/FormValidation'; export { difference, getColor,