Created
November 9, 2012 13:19
-
-
Save webdevbyjoss/4045634 to your computer and use it in GitHub Desktop.
Render error messages of Backbone.Validation for Twitter Bootstrap
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
// Original CoffeScript gist can be found here: https://gist.github.com/2909552 (credits to the author) | |
// I just translated it to JavaScript for my own needs using Coffeescript to Javascript translator | |
_.extend(Backbone.Validation.callbacks, { | |
valid: function(view, attr, selector) { | |
var control, group; | |
control = view.$('[' + selector + '=' + attr + ']'); | |
group = control.parents(".control-group"); | |
group.removeClass("error"); | |
if (control.data("error-style") === "tooltip") { | |
if (control.data("tooltip")) { | |
return control.tooltip("hide"); | |
} | |
} else if (control.data("error-style") === "inline") { | |
return group.find(".help-inline.error-message").remove(); | |
} else { | |
return group.find(".help-block.error-message").remove(); | |
} | |
}, | |
invalid: function(view, attr, error, selector) { | |
var control, group, position, target; | |
control = view.$('[' + selector + '=' + attr + ']'); | |
group = control.parents(".control-group"); | |
group.addClass("error"); | |
if (control.data("error-style") === "tooltip") { | |
position = control.data("tooltip-position") || "right"; | |
control.tooltip({ | |
placement: position, | |
trigger: "manual", | |
title: error | |
}); | |
return control.tooltip("show"); | |
} else if (control.data("error-style") === "inline") { | |
if (group.find(".help-inline").length === 0) { | |
group.find(".controls").append("<span class=\"help-inline error-message\"></span>"); | |
} | |
target = group.find(".help-inline"); | |
return target.text(error); | |
} else { | |
if (group.find(".help-block").length === 0) { | |
group.find(".controls").append("<p class=\"help-block error-message\"></p>"); | |
} | |
target = group.find(".help-block"); | |
return target.text(error); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment