Created
December 29, 2016 17:34
-
-
Save davidmintz/e932074ad4fb45892fcdf63a961bf3c3 to your computer and use it in GitHub Desktop.
a js function that displays validation error messages from an object whose structure matches that returned by Zend\Form\Form::getMessages()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
displayValidationErrors = function(validationErrors) { | |
$('.validation-error').empty(); | |
for (var field in validationErrors) { | |
//console.log("examining field "+field); | |
for (var key in validationErrors[field]) { | |
// console.log("examining key "+key); | |
var message = validationErrors[field][key]; | |
var element = $('#' +field); | |
var errorDiv = $('#error_'+field); | |
if (! errorDiv.length) { errorDiv = null;} | |
if (! element.length) { console.log("is there no element #"+field+ " ?"); | |
// look for an existing div by id | |
if ($('#error_'+field).length) { | |
$('#error_'+field).html(message); | |
} else { | |
console.warn("no element with id "+field + ", and nowhere to put message "+message); | |
} | |
} else { | |
errorDiv = errorDiv || element.next('.validation-error'); | |
if (! errorDiv.length) { | |
errorDiv = $('<div/>') | |
.addClass('alert alert-warning validation-error') | |
.attr({id:'error_'+field}) | |
.insertAfter(element); | |
} | |
errorDiv.html(message).show(); | |
} | |
break; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment