Skip to content

Commit

Permalink
feat(composerjs): allow errors display for collectionField
Browse files Browse the repository at this point in the history
  • Loading branch information
Daric971 committed Oct 28, 2024
1 parent 80c5a1c commit 702ecae
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions assets/js/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ PageComposer.prototype = {
const field = event.form.querySelector(`[name="${violation.propertyPath}"]`);

if (!field) {
this.handleBlockCollectionErrors(violation);

return;
}

Expand Down Expand Up @@ -299,6 +301,60 @@ PageComposer.prototype = {
});
},

/**
* In case the field is a collection,
* we need to go directly to the `.form-group` in order to display the error.
*
* @param violation
*/
handleBlockCollectionErrors(violation) {
const collectionId = `sonata-ba-field-container-${violation.propertyPath
.replace(/\[/g, '_')
.replace(/\]/g, '')
.replace(/__+/g, '___')}`;

const formGroup = document.getElementById(collectionId);

if (!formGroup.classList.contains('form-group')) {
return;
}

let errorList = formGroup.querySelector(
'.help-block.sonata-ba-field-error-messages .list-unstyled .text-danger'
);

if (!errorList) {
const errorWrapper = document.createElement('div');
errorWrapper.classList.add('help-block');
errorWrapper.classList.add('sonata-ba-field-error-messages');

errorList = document.createElement('ul');
errorList.classList.add('list-unstyled');
errorList.classList.add('text-danger');

formGroup.firstElementChild.classList.add('text-danger');

errorWrapper.appendChild(errorList);
formGroup.appendChild(errorWrapper);
}

const errorItem = this.createErrorItem(violation.title);

errorList.appendChild(errorItem);
},

/**
* Create a new error item.
* @param title
*
* @returns {HTMLLIElement}
*/
createErrorItem(title) {
const errorItem = document.createElement('li');
errorItem.innerHTML = `<i class="fas fa-exclamation-circle" aria-hidden="true"></i> ${title}`;
return errorItem;
},

/**
* Compute child count for the given block container id.
*
Expand Down
Loading

0 comments on commit 702ecae

Please sign in to comment.