1(function($) { 2 3 $.fn.addTriggersToJqueryValidate = function() { 4 5 // Loop thru the elements that we jQuery validate is attached to 6 // and return the loop, so jQuery function chaining will work. 7 return this.each(function(){ 8 var form = $(this); 9 10 // Grab this element's validator object (if it has one) 11 var validator = form.data('validator'); 12 13 // Only run this code if there's a validator associated with this element 14 if (! validator) 15 return; 16 17 // Only add these triggers to each element once 18 if (form.data('jQueryValidateTriggersAdded')) 19 return; 20 else 21 form.data('jQueryValidateTriggersAdded', true); 22 23 // Override the function that validates the whole form to trigger a 24 // formValidation event and either formValidationSuccess or formValidationError 25 var oldForm = validator.form; 26 validator.form = function() { 27 var result = oldForm.apply(this, arguments); 28 var form = this.currentForm; 29 $(form).trigger((result == true) ? 'formValidationSuccess' : 'formValidationError', form); 30 $(form).trigger('formValidation', [form, result]); 31 return result; 32 }; 33 34 // Override the function that validates the whole element to trigger a 35 // elementValidation event and either elementValidationSuccess or elementValidationError 36 var oldElement = validator.element; 37 validator.element = function(element) { 38 var result = oldElement.apply(this, arguments); 39 $(element).trigger((result == true) ? 'elementValidationSuccess' : 'elementValidationError', element); 40 $(element).trigger('elementValidation', [element, result]); 41 return result; 42 }; 43 }); 44 }; 45 46 /* Below here are helper methods for calling .bind() for you */ 47 48 $.fn.extend({ 49 50 // Wouldn't it be nice if, when the full form's validation runs, it triggers the 51 // element* validation events? Well, that's what this does! 52 // 53 // NOTE: This is VERY coupled with jquery.validation.unobtrusive and uses its 54 // element attributes to figure out which fields use validation and 55 // whether or not they're currently valid. 56 // 57 triggerElementValidationsOnFormValidation: function() { 58 return this.each(function(){ 59 $(this).bind('formValidation', function(e, form, result){ 60 $(form).find('*[data-val=true]').each(function(i, field){ 61 if ($(field).hasClass('input-validation-error')) { 62 $(field).trigger('elementValidationError', field); 63 $(field).trigger('elementValidation', [field, false]); 64 } else { 65 $(field).trigger('elementValidationSuccess', field); 66 $(field).trigger('elementValidation', [field, true]); 67 } 68 }); 69 }); 70 }); 71 }, 72 73 formValidation: function(fn) { 74 return this.each(function(){ 75 $(this).bind('formValidation', function(e, element, result){ fn(element, result); }); 76 }); 77 }, 78 79 formValidationSuccess: function(fn) { 80 return this.each(function(){ 81 $(this).bind('formValidationSuccess', function(e, element){ fn(element); }); 82 }); 83 }, 84 85 formValidationError: function(fn) { 86 return this.each(function(){ 87 $(this).bind('formValidationError', function(e, element){ fn(element); }); 88 }); 89 }, 90 91 formValidAndInvalid: function(valid, invalid) { 92 return this.each(function(){ 93 $(this).bind('formValidationSuccess', function(e, element){ valid(element); }); 94 $(this).bind('formValidationError', function(e, element){ invalid(element); }); 95 }); 96 }, 97 98 elementValidation: function(fn) { 99 return this.each(function(){ 100 $(this).bind('elementValidation', function(e, element, result){ fn(element, result); }); 101 }); 102 }, 103 104 elementValidationSuccess: function(fn) { 105 return this.each(function(){ 106 $(this).bind('elementValidationSuccess', function(e, element){ fn(element); }); 107 }); 108 }, 109 110 elementValidationError: function(fn) { 111 return this.each(function(){ 112 $(this).bind('elementValidationError', function(e, element){ fn(element); }); 113 }); 114 }, 115
116 elementValidAndInvalid: function(valid, invalid) { 117 return this.each(function(){ 118 $(this).bind('elementValidationSuccess', function(e, element){ valid(element); }); 119 $(this).bind('elementValidationError', function(e, element){ invalid(element); }); 120 }); 121 } 122 123 }); 124 125})(jQuery);
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.