From 8f1a17142bb65cdcf88b8d091769b81a9f7fbb1c Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 10 Mar 2021 09:24:39 +0100 Subject: [PATCH] fix: [js:bootstrap-helper] Make sure button exists --- webroot/js/bootstrap-helper.js | 55 ++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/webroot/js/bootstrap-helper.js b/webroot/js/bootstrap-helper.js index 546e20d..26c6ecb 100644 --- a/webroot/js/bootstrap-helper.js +++ b/webroot/js/bootstrap-helper.js @@ -48,6 +48,7 @@ class UIFactory { /** * Creates and displays a modal where the modal's content is fetched from the provided URL. Reloads the table after a successful operation and handles displayOnSuccess option * @param {string} url - The URL from which the modal's content should be fetched + * @param {string} reloadUrl - The URL from which the data should be fetched after confirming * @param {string} tableId - The table ID which should be reloaded on success * @return {Promise} Promise object resolving to the ModalFactory object */ @@ -588,33 +589,35 @@ class ModalFactory { /** Attach the submission click listener for modals that have been generated by raw HTML */ findSubmitButtonAndAddListener(clearOnclick=true) { const $submitButton = this.$modal.find('.modal-footer #submitButton') - const formID = $submitButton.data('form-id') - let $form - if (formID) { - $form = $(formID) - } else { - $form = this.$modal.find('form') + if ($submitButton[0]) { + const formID = $submitButton.data('form-id') + let $form + if (formID) { + $form = $(formID) + } else { + $form = this.$modal.find('form') + } + if (clearOnclick) { + $submitButton[0].removeAttribute('onclick') + } + + this.options.APIConfirm = (tmpApi) => { + return tmpApi.postForm($form[0]) + .then((data) => { + if (data.success) { + this.options.POSTSuccessCallback(data) + } else { // Validation error + this.injectFormValidationFeedback(form, data.errors) + return Promise.reject('Validation error'); + } + }) + .catch((errorMessage) => { + this.options.POSTFailCallback(errorMessage) + return Promise.reject(errorMessage); + }) + } + $submitButton.click(this.getConfirmationHandlerFunction()) } - if (clearOnclick) { - $submitButton[0].removeAttribute('onclick') - } - - this.options.APIConfirm = (tmpApi) => { - return tmpApi.postForm($form[0]) - .then((data) => { - if (data.success) { - this.options.POSTSuccessCallback(data) - } else { // Validation error - this.injectFormValidationFeedback(form, data.errors) - return Promise.reject('Validation error'); - } - }) - .catch((errorMessage) => { - this.options.POSTFailCallback(errorMessage) - return Promise.reject(errorMessage); - }) - } - $submitButton.click(this.getConfirmationHandlerFunction()) } }