Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(composerjs): allow errors display for collectionField #1781

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 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 All @@ -292,13 +294,66 @@ PageComposer.prototype = {
formGroup.appendChild(errorWrapper);
}

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

errorList.appendChild(errorItem);
});
},

/**
* 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