8

The below code blocks any non-ajax form from submitting.

$(this.form)
  .on('submit', function(event) {
    if (/* Some condition */) {
      event.preventDefault();
    }
  });

In Drupal 8, how can we block an Ajax form from being submitted?

Ajax forms do not trigger a form submit handler and I have not been able to block the click event on an Ajaxified form's submit buttons.

For reference, I am trying to fix Issue #3010084: Form submission finishes before file upload is complete

13

I found the solution here.

// Add submit handler to form.beforeSend.
// Update Drupal.Ajax.prototype.beforeSend only once.
if (typeof Drupal.Ajax !== 'undefined' && typeof Drupal.Ajax.prototype.beforeSubmitOriginal === 'undefined') {
  Drupal.Ajax.prototype.beforeSubmitOriginal = Drupal.Ajax.prototype.beforeSubmit;
  Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) {
    if (/* Custom condition */) {
      this.ajaxing = false;
      return false;
    }
    return this.beforeSubmitOriginal();
  };
}
  • Could you expand on your solution ? How do you implement this code ? I know php and JS but I don't know how AJAX is working in Drupal since I do mostly integration and theming. – Sébastien Gicquel Mar 26 at 9:41
  • I've used this code in my projet and it didn't work. Then, I've copied the code from your patch drupal.org/files/issues/2018-11-19/2952233-5.patch and it works. But I'm still interested in an explanation because I'm not sure to understand what it does. – Sébastien Gicquel Mar 26 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.