MDL-70962 forms: catch modal exceptions when getting body content.

They are thrown in the following circumstances:

 * The dynamic form class doesn't exist;
 * It does exist but it's `check_access` method throws exception

Co-Authored-By: Andrew Nicols <andrew@nicols.co.uk>
This commit is contained in:
Paul Holden 2021-02-22 11:23:28 +00:00
parent 4c26696e7e
commit 11099c15bb
6 changed files with 23 additions and 9 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -456,7 +456,11 @@ define([
// This is a non-spec feature of jQuery and cannot be produced with spec promises. // This is a non-spec feature of jQuery and cannot be produced with spec promises.
// We can encourage people to migrate to this approach, and in future we can swap // We can encourage people to migrate to this approach, and in future we can swap
// it so that setBody() calls setBodyPromise(). // it so that setBody() calls setBodyPromise().
return promise.then(({html, js}) => this.setBody($.when(html, js))); return promise.then(({html, js}) => this.setBody($.when(html, js)))
.catch(exception => {
this.hide();
throw exception;
});
}; };
/** /**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -124,7 +124,9 @@ export default class ModalForm {
// we need to make sure that the modal already exists when we render the form. Some form elements // we need to make sure that the modal already exists when we render the form. Some form elements
// such as date_selector inspect the existing elements on the page to find the highest z-index. // such as date_selector inspect the existing elements on the page to find the highest z-index.
const formParams = new URLSearchParams(Object.entries(this.config.args || {})); const formParams = new URLSearchParams(Object.entries(this.config.args || {}));
this.modal.setBodyContent(this.getBody(formParams.toString())); const bodyContent = this.getBody(formParams.toString());
this.modal.setBodyContent(bodyContent);
bodyContent.catch(Notification.exception);
// After successfull submit, when we press "Cancel" or close the dialogue by clicking on X in the top right corner. // After successfull submit, when we press "Cancel" or close the dialogue by clicking on X in the top right corner.
this.modal.getRoot().on(ModalEvents.hidden, () => { this.modal.getRoot().on(ModalEvents.hidden, () => {
@ -270,8 +272,12 @@ export default class ModalForm {
notifyResetFormChanges() { notifyResetFormChanges() {
return new Promise(resolve => { return new Promise(resolve => {
Y.use('event', 'moodle-core-event', 'moodle-core-formchangechecker', () => { Y.use('event', 'moodle-core-event', 'moodle-core-formchangechecker', () => {
Event.notifyFormSubmitAjax(this.modal.getRoot().find('form')[0], true); // Ensure that modal contains a form element (it may not if the form class threw an early exception).
M.core_formchangechecker.reset_form_dirty_state(); const form = this.modal.getRoot().find('form')[0];
if (form) {
Event.notifyFormSubmitAjax(form, true);
M.core_formchangechecker.reset_form_dirty_state();
}
resolve(); resolve();
}); });
}); });
@ -307,7 +313,11 @@ export default class ModalForm {
let formData = this.modal.getRoot().find('form').serialize(); let formData = this.modal.getRoot().find('form').serialize();
formData = formData + '&' + encodeURIComponent(button.getAttribute('name')) + '=' + formData = formData + '&' + encodeURIComponent(button.getAttribute('name')) + '=' +
encodeURIComponent(button.getAttribute('value')); encodeURIComponent(button.getAttribute('value'));
this.modal.setBodyContent(this.getBody(formData));
const bodyContent = this.getBody(formData);
this.modal.setBodyContent(bodyContent);
bodyContent.catch(Notification.exception);
return null; return null;
}) })
.catch(null); .catch(null);