1/** 2 * plugin for ajax form validation 3 */ 4$.fn.asyncFormValidation = function( customOptions) { 5 // form element 6 var container = this; 7 var containerID = container.attr('id'); 8 isValid = false; 9 10 var options = { 11 errorClass: 'errors' 12 }; 13 $.extend( options, customOptions); 14 15 var listInputs = container.find("input, textarea, select, .input").not(":submit"); 16 17 listInputs.blur( function() { 18 19 container.validate( $(this), false); 20 }); 21 22 listInputs.bind('click keydown blur', function() { 23 $('.'+options.errorClass).each( function() { 24 var parentFormID = $(this).parents('form:first').attr( 'id'); 25 if( parentFormID != containerID) { 26 $(this).remove(); 27 } 28 }); 29 }); 30 31 container.find("input:submit,button.submit").click( function() { 32 33 container.validate( $(this), true); 34 return isValid; 35 }); 36 37 this.showAllErrors = function( data) { 38 listInputs.each( function() { 39 container.showErrors( $(this), data); 40 }); 41 } 42 43 /** 44 * Provadi validaci daneho prvku 45 * 46 * @param elementId 47 * @return 48 */ 49 this.validate = function( element, submit) { 50 51 // serializace formularovych dat 52 var data = container.serialize(); 53 // poslani ajaxoveho dotazu 54 $.ajax({ 55 url: options.url, 56 data: data, 57 type: 'post', 58 dateType: 'json', 59 async: !submit, // ceka se jen pri submitu 60 success: function( response) { 61 62 isValid = !($(response).size()); 63 if( typeof( response) == 'string' && !isValid) { 64 alert( response); 65 } else { 66 if( submit == true) { 67 listInputs.each( function() { 68 container.showErrors( $(this), response); 69 }); 70 } else { 71 72 if( element.attr('type') == 'password') { 73 container.find( 'input[type=password]').each( function() { 74 container.showErrors( $(this), response); 75 }); 76 } else { 77 container.showErrors( element, response); 78 } 79 80 } 81 container.showFormErrors( response, submit); 82 } 83 } 84 }); 85 }; 86 87 /** 88 * pro dany element vypise vygenerovany seznam hlasek 89 */ 90 this.showErrors = function( element, data) { 91 92 var elementId = element.attr('id'); 93 var dashPos = strpos( elementId, '-'); 94 if( dashPos !== false) { 95 elementId = elementId.substr( 0, dashPos); 96 } 97 98 var parent = null; 99 if( element.attr('type') == 'radio') { 100 parent = element.parent().parent(); 101 } else if(element.attr('type') == 'hidden'){ 102 return false; 103 } else { 104 parent = element.parent(); 105 } 106 107 // odstraneni seznamu s chybami 108 parent.find('.' + options.errorClass).remove(); 109 element.removeClass( 'error'); 110 111 // vlozeni noveho seznamu 112 var code = this.getErrorHtml(data[elementId], elementId); 113 if( code != "") { 114 element.addClass( 'error'); 115 } 116 parent.append( code); 117 }; 118 119 /** 120 * pri submitu vypise na zacatku formulare hlasku pokud neco neni ok 121 */ 122 this.showFormErrors = function( data, submit) { 123 124 var messenger = $('.pageerrorcontainer'); 125 var message = "FormuláŠobsahuje chyby. PÅed potvrzenÃm je opravte."; 126 127 messenger.removeClass( 'message_red').hide(); 128 if( !isValid && submit) { 129 messenger.html( message).addClass('message_red').show(); 130 } 131 }; 132 133 /** 134 * Vytvori seznam errorovych hlasek pro dany element 135 * 136 * @param array formErrors Vsechny hlasky prislusici k danemu elementu 137 * @param string id Id inputu 138 * @return string 139 */ 140 this.getErrorHtml = function(formErrors , id) 141 { 142 var code = ""; 143 if( $(formErrors).size()) { 144 code += '<ul id="'+options.errorClass+'-'+id+'" class="'+options.errorClass+'">'; 145 for(errorKey in formErrors) 146 { 147 code += '<li>' + formErrors[errorKey] + '</li>'; 148 } 149 code += '</ul>'; 150 } 151 return code; 152 }; 153 154 return this; 155}; 156 157
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.