Created
March 26, 2012 03:51
-
-
Save paul-english/2202793 to your computer and use it in GitHub Desktop.
Monkey patches that are designed to help jquery-validate work well with 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
# These patches help make the jquery validator work well with | |
# the Twitter Bootstrap html & class conventions | |
# | |
# MONKEY-PATCH warning | |
# These may need to be adjusted if upgrading or changing jquery-validate | |
# from 1.9.0 | |
$.validator.setDefaults | |
errorClass: "error" | |
validClass: "success" | |
errorElement: "span" | |
highlight: (element, errorClass, validClass)-> | |
$obj = if (element.type == 'radio') then this.findByName(element.name) else $(element) | |
$obj.parents("div.control-group").removeClass(validClass).addClass(errorClass) | |
unhighlight: (element, errorClass, validClass)-> | |
$obj = if (element.type == 'radio') then this.findByName(element.name) else $(element) | |
$obj.parents("div.control-group").removeClass(errorClass).addClass(validClass) | |
$.validator.errors = ()-> | |
errorClass = @settings.errorClass.replace(" ", ".") | |
$ @settings.errorElement + "." + errorClass, @errorContext | |
$.validator.showLabel = ()-> | |
label = @errorsFor(element) | |
if label.length | |
# refresh error/success class | |
label.removeClass(@settings.validClass).addClass @settings.errorClass | |
# check if we have a generated label, replace the message then | |
label.attr("generated") and label.html(message) | |
else | |
# create label | |
label = $("<" + @settings.errorElement + "/>").attr( | |
for: @idOrName(element) | |
generated: true | |
).addClass(@settings.errorClass).html(message or "").addClass('help-inline') | |
# make sure the element is visible, even in IE | |
# actually showing the wrapped element is handled elsewhere | |
label = label.hide().show().wrap("<" + @settings.wrapper + "/>").parent() if @settings.wrapper | |
(if @settings.errorPlacement then @settings.errorPlacement(label, $(element)) else label.insertAfter(element)) unless @labelContainer.append(label).length | |
if not message and @settings.success | |
label.text "" | |
(if typeof @settings.success is "string" then label.addClass(@settings.success) else @settings.success(label)) | |
@toShow = @toShow.add(label) |
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
(function() { | |
$.validator.setDefaults({ | |
errorClass: "error", | |
validClass: "success", | |
errorElement: "span", | |
highlight: function(element, errorClass, validClass) { | |
var $obj; | |
$obj = element.type === 'radio' ? this.findByName(element.name) : $(element); | |
return $obj.parents("div.control-group").removeClass(validClass).addClass(errorClass); | |
}, | |
unhighlight: function(element, errorClass, validClass) { | |
var $obj; | |
$obj = element.type === 'radio' ? this.findByName(element.name) : $(element); | |
return $obj.parents("div.control-group").removeClass(errorClass).addClass(validClass); | |
} | |
}); | |
$.validator.errors = function() { | |
var errorClass; | |
errorClass = this.settings.errorClass.replace(" ", "."); | |
return $(this.settings.errorElement + "." + errorClass, this.errorContext); | |
}; | |
$.validator.showLabel = function() { | |
var label; | |
label = this.errorsFor(element); | |
if (label.length) { | |
label.removeClass(this.settings.validClass).addClass(this.settings.errorClass); | |
label.attr("generated") && label.html(message); | |
} else { | |
label = $("<" + this.settings.errorElement + "/>").attr({ | |
"for": this.idOrName(element), | |
generated: true | |
}).addClass(this.settings.errorClass).html(message || "").addClass('help-inline'); | |
if (this.settings.wrapper) { | |
label = label.hide().show().wrap("<" + this.settings.wrapper + "/>").parent(); | |
} | |
if (!this.labelContainer.append(label).length) { | |
if (this.settings.errorPlacement) { | |
this.settings.errorPlacement(label, $(element)); | |
} else { | |
label.insertAfter(element); | |
} | |
} | |
} | |
if (!message && this.settings.success) { | |
label.text(""); | |
if (typeof this.settings.success === "string") { | |
label.addClass(this.settings.success); | |
} else { | |
this.settings.success(label); | |
} | |
} | |
return this.toShow = this.toShow.add(label); | |
}; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment