From ce09691a357c16b6446f5bc6e56a7910335f3fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Zaeffferer?= Date: Mon, 30 Nov 2009 18:25:47 +0000 Subject: [PATCH] * Added automatic detection of substitution parameters in messages, removing the need to provide format functions (http://plugins.jquery.com/node/11195) --- changelog.txt | 1 + jquery.validate.js | 9 +++++++-- test/test.js | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index 3a83980..d0df272 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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 --- diff --git a/jquery.validate.js b/jquery.validate.js index 1aa10f6..9cadd4d 100644 --- a/jquery.validate.js +++ b/jquery.validate.js @@ -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; }, diff --git a/test/test.js b/test/test.js index ef619bd..51a79f2 100644 --- a/test/test.js +++ b/test/test.js @@ -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");