Skip to content

Commit

Permalink
Core: Added support for generic messages via data-msg attribute
Browse files Browse the repository at this point in the history
Closes gh-747
  • Loading branch information
doryphores authored and jzaefferer committed Jan 14, 2014
1 parent 279b932 commit 5bebaa5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,10 @@ $.extend($.validator, {

// return the custom message for the given element and validation method
// specified in the element's HTML5 data attribute
// return the generic message if present and no method specific message is present
customDataMessage: function( element, method ) {
return $( element ).data( "msg" + method[ 0 ].toUpperCase() + method.substring( 1 ).toLowerCase() );

return $( element ).data( "msg" + method[ 0 ].toUpperCase() +
method.substring( 1 ).toLowerCase() ) || $( element ).data("msg");
},

// return the custom message for the given element name and validation method
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ <h2 id="qunit-userAgent"></h2>

<form id="testForm9">
<input id="testEmail9" data-rule-required="true" data-rule-email="true" data-msg-required="required" data-msg-email="email" />
<input id="testGeneric9" data-rule-required="true" data-rule-email="true" data-msg="generic" data-msg-email="email" />
</form>

<form id="testForm10">
Expand Down
10 changes: 8 additions & 2 deletions test/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ test("read messages from metadata", function() {
form.validate();
var e = $("#testEmail9");
e.valid();
equal( form.find("label").text(), "required" );
equal( form.find("label[for=testEmail9]").text(), "required" );
e.val("bla").valid();
equal( form.find("label").text(), "email" );
equal( form.find("label[for=testEmail9]").text(), "email" );

var g = $("#testGeneric9");
g.valid();
equal( form.find("label[for=testGeneric9]").text(), "generic");
g.val("bla").valid();
equal( form.find("label[for=testGeneric9]").text(), "email" );
});


Expand Down

0 comments on commit 5bebaa5

Please sign in to comment.