Hi,

I would like to have the ability to handle the submission of the form myself after client side validation has been completed.

My approach is to add a submitHandler into the validation options and the relevant admin interface to flag it on and off.

I will submit an attempted patch soon.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

roflcopterDorrie created an issue. See original summary.

roflcopterDorrie’s picture

roflcopterDorrie’s picture

An example of the submit handler in action:

$('#webform-client-form-614').bind('clientsideValidationFormDoesNotHaveErrors', function(form, event) {
  if (foo == bar) {
    return true;
  } else {
    alert("Something is not right and form won't be submitted");
    return false;
  }
});
welly’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.83 KB

Tested this on a project that I needed this functionality working on. Works great! The only thing I would suggest is the bind event name 'clientsideValidationFormDoesNotHaveErrors' is a little unwieldy. I've updated the patch with a slightly more concise bind event name 'submitHandler', see attached. But aside from that, great patch!

Edit. as an after thought, the bind event probably needs something suggesting it's specifically for clientside validation. But something a little more concise than the original all the same!

hugronaphor’s picture

Version: 7.x-1.42 » 7.x-2.x-dev
FileSize
2.75 KB

The patch works great.

Regarding the event name, I think we should follow the existing names style so: "clientsideValidationFormSubmitHandler" should be ok.

As I'm using the 2.x version, I'll leave here the patch for the 7.x-2.x branch

And an example to have it globally:

;(function ($) {
  'use strict';

  Drupal.behaviors.CVValiadation = {
    attach: function (context, settings) {

      if (typeof (Drupal.settings.clientsideValidation.forms) === 'undefined') {
        return;
      }

      for (var i in Drupal.settings.clientsideValidation.forms) {
        if (Drupal.settings.clientsideValidation.forms.hasOwnProperty(i)) {
          $('#' + i).bind('clientsideValidationFormSubmitHandler', function (e, form, event) {
            // Check if there are any errors.
            var errors = $(form).find('.error:visible');
            // can't get the value using:
            // self.validator.errorList
            // self.validator.numberOfInvalids()

            // Check if we have errors.
            if (!(errors.length)) {
              form.submit();
            }
          });
        }
      }
    }
  };
}(jQuery));
khiminrm’s picture

I've improved patch from #5, because when there is no custom submit handler - form will not be submitted.

khiminrm’s picture

FileSize
893 bytes
khiminrm’s picture

Fixed name of event in description of new setting.

khiminrm’s picture

Issue summary: View changes

patch from #8 can break ajax forms even if don't bind 'clientsideValidationFormSubmitHandler' event with it.
I think we must not submit form e.g form.sumit(); in submitHandler, but need to prevent submission or return true.
I've used idea from https://www.drupal.org/project/clientside_validation/issues/2898885 and added submitHandler
and created custom behavior for specific form with some class:

Drupal.behaviors.myCustomSubmitHandler = {
    attach: function (context, settings) {
      $('form.my-test-form').once('add-custom-submit-handler', function () {
        $(document).bind('clientsideValidationInitialized', function () {
          let formId = $('form.my-test-form').attr('id');
            if (formId !== undefined && formId !== '' && Drupal.cvInstances !== undefined && Drupal.cvInstances[formId] !== undefined) {
              validator.settings.submitHandler = function (form, event) {
              if (!$(form).hasClass('can-be-submitted')) {
                event.preventDefault();
              }
              else {
                return true;
              }
          }
        };
      });
    }
  };
khiminrm’s picture

Status: Reviewed & tested by the community » Needs work