app/assets/javascripts/zutils.js in zutils-0.2.8 vs app/assets/javascripts/zutils.js in zutils-0.2.9

- old
+ new

@@ -24,28 +24,42 @@ $.rails.enableFormElements($("#new_" + $(item).data('model') + "")); }); $("#new_" + $(item).data('model')).on("submit", function(e) { e.preventDefault(); + $("#new_" + $(item).data('model')).find('.has-error').removeClass('has-error'); + $("#new_" + $(item).data('model')).find('.help-block').text(""); $.ajax({ method: "POST", dataType: "json", url: $(this).attr("action"), data: $(this).serialize(), success: function(response) { selectizeCallback({value: response.id, text: response.name}); selectizeCallback = null; $("." + $(item).data('model') + "-modal").modal('toggle'); + }, + error: function(response) { + $.each(response.responseJSON, function(key, value) { + $("#new_" + $(item).data('model')).find("." + $(item).data('model') + '_' + key).addClass('has-error'); + if (!($("#new_" + $(item).data('model') + " ." + $(item).data('model') + '_' + key).find('.help-block').length)) { + $("#new_" + $(item).data('model')).find("." + $(item).data('model') + '_' + key).append("<p class='help-block'></p>"); + } + $("#new_" + $(item).data('model')).find("." + $(item).data('model') + '_' + key).find('.help-block').text(value); + }); + $.rails.enableFormElements($("#new_" + $(item).data('model') + "")); } }); }); }); $(".selectize_with_add_without_modal").each(function(i, item) { $(item).selectize({ create: function(input, callback) { + $(item).parent().removeClass('has-error'); + $(item).parent().find('.help-block').text(""); selectizeCallback = callback; data = {}; data[$(item).data('model')] = {}; data[$(item).data('model')][$(item).data('field')] = input; $.ajax({ @@ -53,9 +67,17 @@ dataType: "json", url: "/" + $(item).data('pluralized'), data: data, success: function(response) { selectizeCallback({value: response.id, text: response.name}); + }, + error: function(response) { + $(item).parent().addClass('has-error'); + if (!( $(item).parent().find('.help-block').length)) { + $(item).parent().append("<p class='help-block'></p>"); + } + $(item).parent().find('.help-block').text("Valor já existente: " + input); + selectizeCallback(); } }); selectizeCallback = callback; } });