Last active
December 31, 2015 14:06
-
-
Save jurigis/ea47c0fb53461308068d to your computer and use it in GitHub Desktop.
OctoberCms. Shows ajax validation errors in Backend.
This file contains hidden or 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
(function(){ | |
function showFieldsValidationMessages(jqXHR, $target) { | |
if (!$target) { | |
$target = $('form').first(); | |
} | |
if (jqXHR.hasOwnProperty('responseJSON')) { | |
if (jqXHR.responseJSON['X_OCTOBER_ERROR_FIELDS']) { | |
$("span.error").text(""); | |
$("div.form-group").removeClass('has-error'); | |
$("div.form-group").addClass('has-success'); | |
$.each(jqXHR.responseJSON['X_OCTOBER_ERROR_FIELDS'], function(key, val) { | |
var $el = $target.find("div[data-field-name='" + key + "']"); | |
$el.addClass('has-error'); | |
$el.removeClass('has-success'); | |
var $err_container = $el.children("span.error"); | |
if (!$err_container.length) { | |
$err_container = $el.append($(document.createElement('span')).addClass('help-block before-field error').text(val)); | |
} else { | |
$err_container.text(val); | |
} | |
}); | |
} | |
} | |
return false; | |
}; | |
$(document).ready(function() { | |
$(document).on('ajaxError', function(e, target, status, jqXHR) { | |
showFieldsValidationMessages(jqXHR, $(e.target)); | |
}); | |
$(document).on('ajaxSuccess', function(e, target, status, jqXHR) { | |
$("span.error").text(""); | |
$("div.form-group").removeClass('has-error'); | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed with additional has-success class.