Skip to content

Commit cb55bbe

Browse files
committed
feat(composerjs): allow errors display for collectionField
1 parent 80c5a1c commit cb55bbe

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

assets/js/composer.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ PageComposer.prototype = {
269269
const field = event.form.querySelector(`[name="${violation.propertyPath}"]`);
270270

271271
if (!field) {
272+
this.handleBlockCollectionErrors(violation);
273+
272274
return;
273275
}
274276

@@ -292,13 +294,66 @@ PageComposer.prototype = {
292294
formGroup.appendChild(errorWrapper);
293295
}
294296

295-
const errorItem = document.createElement('li');
296-
errorItem.innerHTML = `<i class="fas fa-exclamation-circle" aria-hidden="true"></i> ${violation.title}`;
297+
const errorItem = this.createErrorItem(violation.title);
297298

298299
errorList.appendChild(errorItem);
299300
});
300301
},
301302

303+
/**
304+
* In case the field is a collection,
305+
* we need to go directly to the `.form-group` in order to display the error.
306+
*
307+
* @param violation
308+
*/
309+
handleBlockCollectionErrors(violation) {
310+
const collectionId = `sonata-ba-field-container-${violation.propertyPath
311+
.replace(/\[/g, '_')
312+
.replace(/\]/g, '')
313+
.replace(/__+/g, '___')}`;
314+
315+
const formGroup = document.getElementById(collectionId);
316+
317+
if (!formGroup.classList.contains('form-group')) {
318+
return;
319+
}
320+
321+
let errorList = formGroup.querySelector(
322+
'.help-block.sonata-ba-field-error-messages .list-unstyled .text-danger'
323+
);
324+
325+
if (!errorList) {
326+
const errorWrapper = document.createElement('div');
327+
errorWrapper.classList.add('help-block');
328+
errorWrapper.classList.add('sonata-ba-field-error-messages');
329+
330+
errorList = document.createElement('ul');
331+
errorList.classList.add('list-unstyled');
332+
errorList.classList.add('text-danger');
333+
334+
formGroup.firstElementChild.classList.add('text-danger');
335+
336+
errorWrapper.appendChild(errorList);
337+
formGroup.appendChild(errorWrapper);
338+
}
339+
340+
const errorItem = this.createErrorItem(violation.title);
341+
342+
errorList.appendChild(errorItem);
343+
},
344+
345+
/**
346+
* Create a new error item.
347+
* @param title
348+
*
349+
* @returns {HTMLLIElement}
350+
*/
351+
createErrorItem(title) {
352+
const errorItem = document.createElement('li');
353+
errorItem.innerHTML = `<i class="fas fa-exclamation-circle" aria-hidden="true"></i> ${title}`;
354+
return errorItem;
355+
},
356+
302357
/**
303358
* Compute child count for the given block container id.
304359
*

0 commit comments

Comments
 (0)