|
1 | 1 | // CUSTOMIZATION: |
2 | 2 | // - added warning state to form |
3 | 3 | // - backport for https://github.com/collective/volto-form-block/pull/122 |
4 | | - |
| 4 | +// - handle field errors coming from backend |
5 | 5 | import React, { useState, useEffect, useReducer, useRef } from 'react'; |
6 | 6 | import { useSelector, useDispatch } from 'react-redux'; |
7 | 7 | import PropTypes from 'prop-types'; |
@@ -141,7 +141,6 @@ const View = ({ data, id, path }) => { |
141 | 141 |
|
142 | 142 | const [formState, setFormState] = useReducer(formStateReducer, initialState); |
143 | 143 | const [formErrors, setFormErrors] = useState([]); |
144 | | - console.log(formErrors); |
145 | 144 |
|
146 | 145 | const submitResults = useSelector( |
147 | 146 | (state) => state.submitForm?.subrequests?.[id], |
@@ -360,9 +359,15 @@ const View = ({ data, id, path }) => { |
360 | 359 | let errorDescription = `${ |
361 | 360 | JSON.parse(submitResults.error.response?.text ?? '{}')?.message |
362 | 361 | }`; |
363 | | - const fieldsErrors = JSON.parse( |
364 | | - submitResults.error.response?.text ?? '{}', |
365 | | - )?.fields_errors; |
| 362 | + |
| 363 | + //CUSTOM: handle field errors coming from backend |
| 364 | + |
| 365 | + let fieldsErrors = []; |
| 366 | + if (errorDescription.startsWith('[')) { |
| 367 | + fieldsErrors = JSON.parse( |
| 368 | + errorDescription.replaceAll('"', '\\"').replaceAll("'", '"'), |
| 369 | + ); |
| 370 | + } |
366 | 371 |
|
367 | 372 | // fieldsErrors = [ |
368 | 373 | // { field_id: '1767088715579', label: 'univoco', message: 'Errore' }, |
|
0 commit comments