diff --git a/clientside_validation_jquery/js/cv.jquery.validate.js b/clientside_validation_jquery/js/cv.jquery.validate.js index f4c43ce..1d508d7 100644 --- a/clientside_validation_jquery/js/cv.jquery.validate.js +++ b/clientside_validation_jquery/js/cv.jquery.validate.js @@ -5,7 +5,7 @@ (function ($, Drupal, drupalSettings) { /** - * Attaches jQuery validate behavoir to forms. + * Attaches jQuery validate behavior to forms. * * @type {Drupal~behavior} * @@ -25,6 +25,28 @@ $(context).find('form').each(function() { $(this).validate(drupalSettings.cvJqueryValidateOptions); }); + + // Update Drupal.Ajax.prototype.beforeSend only once. + if (typeof Drupal.Ajax !== 'undefined' + && typeof Drupal.Ajax.prototype.beforeSubmitCVOriginal === 'undefined') { + Drupal.Ajax.prototype.beforeSubmitCVOriginal = Drupal.Ajax.prototype.beforeSubmit; + Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) { + if (typeof this.$form !== 'undefined') { + $(this.$form).validate(drupalSettings.cvJqueryValidateOptions); + if (!this.$form.valid()) { + this.ajaxing = false; + this.$form.addClass('clientside-validation-ajax-submit-invalid'); + return false; + } + else { + this.$form.removeClass('clientside-validation-ajax-submit-invalid'); + } + } + + return this.beforeSubmitCVOriginal(form_values, element_settings, options); + }; + } } }; + })(jQuery, Drupal, drupalSettings);