Skip to content

Commit 43cf37f

Browse files
committed
Set errors per form.
1 parent 284b736 commit 43cf37f

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

frontend/packages/volto-form-block/src/components/ViewSchemaForm.jsx

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,19 @@ const FormBlockView = ({ data, id, path, moment: momentlib }) => {
9191
}
9292

9393
// Get captcha error
94-
const captchaError =
95-
localStorage.getItem('formBlockError') === 'error' ? true : false;
94+
let captchaErrors = {};
95+
let captchaError = false;
96+
try {
97+
captchaErrors = JSON.parse(localStorage.getItem('formBlocksError'));
98+
if (!captchaErrors) {
99+
captchaErrors = {};
100+
}
101+
} catch (error) {
102+
captchaErrors = {};
103+
}
104+
if (id in captchaErrors) {
105+
captchaError = captchaErrors[id] === 'error' ? true : false;
106+
}
96107

97108
useEffect(() => {
98109
if (captchaError) {
@@ -215,7 +226,20 @@ const FormBlockView = ({ data, id, path, moment: momentlib }) => {
215226
// Set submitted
216227
setSubmitted(true);
217228
setSubmittedData(submitData);
218-
localStorage.removeItem('formBlockError');
229+
230+
// Clear error
231+
let captchaErrors;
232+
try {
233+
captchaErrors = JSON.parse(localStorage.getItem('formBlocksError'));
234+
if (!captchaErrors) {
235+
captchaErrors = {};
236+
}
237+
} catch (error) {
238+
captchaErrors = {};
239+
}
240+
241+
delete captchaErrors[id];
242+
localStorage.setItem('formBlocks', JSON.stringify(captchaErrors));
219243

220244
// Clear localstorage
221245
let localStorageData;
@@ -226,7 +250,10 @@ const FormBlockView = ({ data, id, path, moment: momentlib }) => {
226250
}
227251

228252
delete localStorageData[id];
229-
localStorage.setItem('formBlocks', JSON.stringify(localStorageData));
253+
localStorage.setItem(
254+
'formBlocksError',
255+
JSON.stringify(localStorageData),
256+
);
230257

231258
// Redirect after submit
232259
if (
@@ -251,7 +278,21 @@ const FormBlockView = ({ data, id, path, moment: momentlib }) => {
251278
if (Array.isArray(errorsList)) {
252279
invariantErrors = extractInvariantErrors(errorsList);
253280
}
254-
localStorage.setItem('formBlockError', 'error');
281+
282+
// Clear error
283+
let captchaErrors = {};
284+
try {
285+
captchaErrors = JSON.parse(localStorage.getItem('formBlocksError'));
286+
if (!captchaErrors) {
287+
captchaErrors = {};
288+
}
289+
} catch (error) {
290+
captchaErrors = {};
291+
}
292+
293+
captchaErrors[id] = 'error';
294+
localStorage.setItem('formBlocksError', JSON.stringify(captchaErrors));
295+
255296
window.location.reload();
256297
/*
257298
if (invariantErrors.length > 0) {

0 commit comments

Comments
 (0)