Merged in changes for specifying custom error messages in data attributes

This commit is contained in:
Max Lynch
2012-05-15 17:34:22 -05:00
parent 55920f0f75
commit 907467e874
3 changed files with 23 additions and 0 deletions

7
jquery.validate.js vendored
View File

@@ -581,6 +581,12 @@ $.extend($.validator, {
return meta && meta.messages && meta.messages[method];
},
// return the custom message for the given element and validation method
// specified in the element's HTML5 data attribute
customDataMessage: function(element, method) {
return $(element).data('msg-' + method.toLowerCase()) || (element.attributes && $(element).attr('data-msg-' + method.toLowerCase()));
},
// return the custom message for the given element name and validation method
customMessage: function( name, method ) {
var m = this.settings.messages[name];
@@ -600,6 +606,7 @@ $.extend($.validator, {
defaultMessage: function( element, method) {
return this.findDefined(
this.customMessage( element.name, method ),
this.customDataMessage( element, method ),
this.customMetaMessage( element, method ),
// title is never undefined, so handle empty string as undefined
!this.settings.ignoreTitle && element.title || undefined,

View File

@@ -154,6 +154,10 @@
<input name="testForm12text" id="testForm12text" class="{required:true}" />
</form>
<form id="dataMessages">
<input name="dataMessagesName" id="dataMessagesName" class="required" data-msg-required="You must enter a value here" />
</form>
<div id="simplecontainer">
<h3></h3>
</div>

View File

@@ -1252,3 +1252,15 @@ test("ignore hidden elements at start", function(){
$('#userForm [name=username]').show();
ok(! validate.form(), "form should be invalid when required element is visible");
});
test("Specify error messages through data attributes", function() {
var form = $('#dataMessages');
var name = $('#dataMessagesName');
var v = form.validate();
form.get(0).reset();
name.valid();
var label = $('#dataMessages label');
equal( label.text(), "You must enter a value here", "Correct error label" );
});