diff --git a/README.rst b/README.rst index 185a35a..47ca68f 100644 --- a/README.rst +++ b/README.rst @@ -212,7 +212,7 @@ Add script to the template from #5 and bind the ``modalForm`` to the trigger ele }); -Async create/update with or without modal closing on submit +Async create/update/delete with or without modal closing on submit =========================================================== Set ``asyncUpdate`` and ``asyncSettings`` settings to create or update objects without page redirection to ``successUrl`` and define whether a modal should close or stay opened after form submission. See comments in example below and paragraph **modalForm options** for explanation of ``asyncSettings``. @@ -234,6 +234,10 @@ Set ``asyncUpdate`` and ``asyncSettings`` settings to create or update objects w + + ... @@ -278,6 +282,26 @@ Set ``asyncUpdate`` and ``asyncSettings`` settings to create or update objects w }); } updateBookModalForm(); + + //checking for form validity on delete means submitting the delete form twice, causing an error + function deleteBookModalForm() { + $(".delete-book").each(function () { + $(this).modalForm({ + formURL: $(this).data("form-url"), + asyncUpdate: true, + checkValidBeforeSubmit: false, + asyncSettings: { + closeOnSubmit: false, + successMessage: asyncSuccessMessage, + dataUrl: "books/", + dataElementId: "#books-table", + dataKey: "table", + addModalFormFunction: deleteBookModalForm + } + }); + }); + } + deleteBookModalForm(); ... }); @@ -338,6 +362,9 @@ errorClass submitBtn Sets the custom class for the button triggering form submission in modal. ``Default: ".submit-btn"`` +checkValidBeforeSubmit + Sets whether to check form validity before submitting (set to false for async delete). ``Default: true`` + asyncUpdate Sets asynchronous content update after form submission. ``Default: false`` diff --git a/bootstrap_modal_forms/static/js/jquery.bootstrap.modal.forms.js b/bootstrap_modal_forms/static/js/jquery.bootstrap.modal.forms.js index c7cdd69..b708e6c 100644 --- a/bootstrap_modal_forms/static/js/jquery.bootstrap.modal.forms.js +++ b/bootstrap_modal_forms/static/js/jquery.bootstrap.modal.forms.js @@ -19,7 +19,12 @@ https://github.com/trco/django-bootstrap-modal-forms var addEventHandlers = function (settings) { // submitBtn click handler $(settings.submitBtn).on("click", function (event) { - isFormValid(settings, submitForm); + if (settings.checkValidBeforeSubmit) { + isFormValid(settings, submitForm); + } + else { + submitForm(settings); + } }); // Modal close handler $(settings.modalID).on("hidden.bs.modal", function (event) { @@ -144,6 +149,7 @@ https://github.com/trco/django-bootstrap-modal-forms formURL: null, errorClass: ".invalid", submitBtn: ".submit-btn", + checkValidBeforeSubmit: true, asyncUpdate: false, asyncSettings: { closeOnSubmit: false, diff --git a/examples/templates/_books_table.html b/examples/templates/_books_table.html index 2cc41b0..62d60fe 100644 --- a/examples/templates/_books_table.html +++ b/examples/templates/_books_table.html @@ -8,7 +8,7 @@ Publication date Pages Price (€) - Read / Update / Delete + Read / Update / Delete / Async Delete @@ -34,6 +34,10 @@ + + {% endfor %} diff --git a/examples/templates/examples/delete_book_async.html b/examples/templates/examples/delete_book_async.html new file mode 100644 index 0000000..af7c1a8 --- /dev/null +++ b/examples/templates/examples/delete_book_async.html @@ -0,0 +1,22 @@ +{% load widget_tweaks %} + +
+ {% csrf_token %} + + + + + + + +
diff --git a/examples/templates/index.html b/examples/templates/index.html index 3f3e62b..9d7699e 100644 --- a/examples/templates/index.html +++ b/examples/templates/index.html @@ -82,19 +82,23 @@

modalID: "#create-modal" }); - var asyncSuccessMessage = [ - "
", - "Success: Book was updated.", - "", - "
", - "