* Added automatic detection of substitution parameters in messages, removing the need to provide format functions (http://plugins.jquery.com/node/11195)

This commit is contained in:
Jörn Zaeffferer
2009-11-30 18:25:47 +00:00
parent f394bbc85a
commit ce09691a35
3 changed files with 27 additions and 2 deletions

View File

@@ -14,6 +14,7 @@
* Fixed default message for digits (http://plugins.jquery.com/node/9853)
* Fixed issue with cached remote message (http://plugins.jquery.com/node/11029 and http://plugins.jquery.com/node/9351)
* Fixed a missing semicolon in additional-methods.js (http://plugins.jquery.com/node/9233)
* Added automatic detection of substitution parameters in messages, removing the need to provide format functions (http://plugins.jquery.com/node/11195)
1.5.5
---

9
jquery.validate.js vendored
View File

@@ -558,13 +558,18 @@ $.extend($.validator, {
},
formatAndAdd: function( element, rule ) {
var message = this.defaultMessage( element, rule.method );
if ( typeof message == "function" )
var message = this.defaultMessage( element, rule.method ),
theregex = /\$?\{(\d+)\}/g;
if ( typeof message == "function" ) {
message = message.call(this, rule.parameters, element);
} else if (theregex.test(message)) {
message = jQuery.format(message.replace(theregex, '{$1}'), rule.parameters);
}
this.errorList.push({
message: message,
element: element
});
this.errorMap[element.name] = message;
this.submitted[element.name] = message;
},

View File

@@ -419,6 +419,25 @@ test("formatAndAdd2", function() {
equals( "element bar is not valid", v.errorList[0].message );
});
test("formatAndAdd, auto detect substitution string", function() {
var v = $("#testForm1clean").validate({
rules: {
firstname: {
required: true,
rangelength: [5, 10]
}
},
messages: {
firstname: {
rangelength: "at least ${0}, up to {1}"
}
}
});
$("#firstnamec").val("abc");
v.form();
equals( "at least 5, up to 10", v.errorList[0].message );
})
test("error containers, simple", function() {
expect(14);
var container = $("#simplecontainer");