Skip to content

Commit 702ecae

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

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

assets/js/composer.js

Lines changed: 56 additions & 0 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

@@ -299,6 +301,60 @@ PageComposer.prototype = {
299301
});
300302
},
301303

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

0 commit comments

Comments
 (0)