1/** 2 * @file 3 * Attaches behaviors for the Clientside Validation jQuery module. 4 */ 5(function ($, Drupal, once, drupalSettings) { 6 'use strict'; 7 8 if (typeof drupalSettings.cvJqueryValidateOptions === 'undefined') { 9 drupalSettings.cvJqueryValidateOptions = {}; 10 } 11 12 if (drupalSettings.clientside_validation_jquery.force_validate_on_blur) { 13 drupalSettings.cvJqueryValidateOptions.onfocusout = function (element) { 14 // "eager" validation 15 this.element(element); 16 }; 17 } 18 19 // Add messages with translations from backend. 20 $.extend($.validator.messages, drupalSettings.clientside_validation_jquery.messages); 21 22 // Overwrite default URL validation method, accepting public/private urlscheme. 23 var _urlValidation = $.validator.methods.url; 24 $.validator.methods.url = function(value, element) { 25 return _urlValidation.call(this, value, element) || /^(public|private):\/\//.test(value); 26 } 27 28 /** 29 * Attaches jQuery validate behavior to forms. 30 * 31 * @type {Drupal~behavior} 32 * 33 * @prop {Drupal~behaviorAttach} attach 34 * Attaches the outline behavior to the right context. 35 */ 36 Drupal.behaviors.cvJqueryValidate = { 37 attach: function (context) { 38 if (typeof Drupal.Ajax !== 'undefined') { 39 // Update Drupal.Ajax.prototype.beforeSend only once. 40 if (typeof Drupal.Ajax.prototype.beforeSubmitCVOriginal === 'undefined') { 41 var validateAll = 2; 42 try { 43 validateAll = drupalSettings.clientside_validation_jquery.validate_all_ajax_forms; 44 } 45 catch(e) { 46 // Do nothing if we do not have settings or value in settings. 47 } 48 49 Drupal.Ajax.prototype.beforeSubmitCVOriginal = Drupal.Ajax.prototype.beforeSubmit; 50 Drupal.Ajax.prototype.beforeSubmit = function (form_values, element_settings, options) { 51 if (typeof this.$form !== 'undefined' && (validateAll === 1 || $(this.element).hasClass('cv-validate-before-ajax')) && $(this.element).attr("formnovalidate") == undefined) { 52 $(this.$form).removeClass('ajax-submit-prevented'); 53 54 // Handle for the HTML5 validation if enabled. 55 var forceHtml5Validation = drupalSettings.clientside_validation_jquery.force_html5_validation; 56 var formId = $(this.$form).attr('id'); 57 if (forceHtml5Validation && formId) { 58 if (!document.getElementById(formId).reportValidity()) { 59 this.ajaxing = false; 60 $(this.$form).addClass('ajax-submit-prevented'); 61 return false; 62 } 63 } 64 65 $(this.$form).validate(); 66 if (!($(this.$form).valid())) { 67 this.ajaxing = false; 68 $(this.$form).addClass('ajax-submit-prevented'); 69 return false; 70 } 71 } 72 73 return this.beforeSubmitCVOriginal.apply(this, arguments); 74 }; 75 } 76 } 77 78 // Allow all modules to update the validate options. 79 // Example of how to do this is shown below. 80 $(document).trigger('cv-jquery-validate-options-update', drupalSettings.cvJqueryValidateOptions); 81 82 // Process for all the forms on the page everytime, 83 // we already use once so we should be good. 84 once('cvJqueryValidate', 'body form').forEach(function(element) { 85 $(element).validate(drupalSettings.cvJqueryValidateOptions); 86 }); 87 } 88 }; 89})(jQuery, Drupal, once, drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.