-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Core: move message processing from formatAndAdd
to defaultMessage
#1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,9 @@ $.validator.format = function( source, params ) { | |
return $.validator.format.apply( this, args ); | ||
}; | ||
} | ||
if ( params === undefined ) { | ||
return source; | ||
} | ||
if ( arguments.length > 2 && params.constructor !== Array ) { | ||
params = $.makeArray( arguments ).slice( 1 ); | ||
} | ||
|
@@ -739,26 +742,29 @@ $.extend( $.validator, { | |
return undefined; | ||
}, | ||
|
||
defaultMessage: function( element, method ) { | ||
return this.findDefined( | ||
this.customMessage( element.name, method ), | ||
this.customDataMessage( element, method ), | ||
|
||
// 'title' is never undefined, so handle empty string as undefined | ||
!this.settings.ignoreTitle && element.title || undefined, | ||
$.validator.messages[ method ], | ||
"<strong>Warning: No message defined for " + element.name + "</strong>" | ||
); | ||
}, | ||
defaultMessage: function( element, rule ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a BC Break. Previusly the second argument was an string and now its an object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (typeof rule === 'string') {
rule = {method: rule}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right!
But I can fix that in a followup proposal to maintain backward compatibility with code that still relaying on the old behavior. Thanks for the note! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can we write custom validation rules then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like any of the additional methos are written There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of them use a remote validation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a PR soon with the following patch: src/core.js | 8 +++++++-
test/test.js | 2 +-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/core.js b/src/core.js
index 743f028..b497c4f 100644
--- a/src/core.js
+++ b/src/core.js
@@ -803,6 +803,10 @@ $.extend( $.validator, {
},
defaultMessage: function( element, rule ) {
+ if ( typeof rule === "string" ) {
+ rule = { method: rule };
+ }
+
var message = this.findDefined(
this.customMessage( element.name, rule.method ),
this.customDataMessage( element, rule.method ),
@@ -1067,10 +1071,12 @@ $.extend( $.validator, {
},
previousValue: function( element, method ) {
+ method = typeof method === "string" && method || "remote";
+
return $.data( element, "previousValue" ) || $.data( element, "previousValue", {
old: null,
valid: true,
- message: this.defaultMessage( element, { method: method } )
+ message: this.defaultMessage( element, method )
} );
},
diff --git a/test/test.js b/test/test.js
index 12541c2..05ec156 100644
--- a/test/test.js
+++ b/test/test.js
@@ -678,7 +678,7 @@ test( "option: errorClass with multiple classes", function() {
test( "defaultMessage(), empty title is ignored", function() {
var v = $( "#userForm" ).validate();
- equal( v.defaultMessage( $( "#username" )[ 0 ], { method: "required", parameters: true } ), "This field is required." );
+ equal( v.defaultMessage( $( "#username" )[ 0 ], "required" ), "This field is required." );
} );
test( "#741: move message processing from formatAndAdd to defaultMessage", function() { If there are other things I should take care off, please mention them and I will update before sending the PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why change May add some kind of deprecation comment about the old parameter type so it could be removed on next major version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. |
||
var message = this.findDefined( | ||
this.customMessage( element.name, rule.method ), | ||
this.customDataMessage( element, rule.method ), | ||
|
||
formatAndAdd: function( element, rule ) { | ||
var message = this.defaultMessage( element, rule.method ), | ||
// 'title' is never undefined, so handle empty string as undefined | ||
!this.settings.ignoreTitle && element.title || undefined, | ||
$.validator.messages[ rule.method ], | ||
"<strong>Warning: No message defined for " + element.name + "</strong>" | ||
), | ||
theregex = /\$?\{(\d+)\}/g; | ||
if ( typeof message === "function" ) { | ||
message = message.call( this, rule.parameters, element ); | ||
} else if ( theregex.test( message ) ) { | ||
message = $.validator.format( message.replace( theregex, "{$1}" ), rule.parameters ); | ||
} | ||
|
||
return message; | ||
}, | ||
|
||
formatAndAdd: function( element, rule ) { | ||
var message = this.defaultMessage( element, rule ); | ||
|
||
this.errorList.push( { | ||
message: message, | ||
element: element, | ||
|
@@ -1004,7 +1010,7 @@ $.extend( $.validator, { | |
return $.data( element, "previousValue" ) || $.data( element, "previousValue", { | ||
old: null, | ||
valid: true, | ||
message: this.defaultMessage( element, "remote" ) | ||
message: this.defaultMessage( element, { method: "remote" } ) | ||
} ); | ||
}, | ||
|
||
|
@@ -1393,8 +1399,8 @@ $.extend( $.validator, { | |
validator.showErrors(); | ||
} else { | ||
errors = {}; | ||
message = response || validator.defaultMessage( element, "remote" ); | ||
errors[ element.name ] = previous.message = $.isFunction( message ) ? message( value ) : message; | ||
message = response || validator.defaultMessage( element, { method: "remote", parameters: value } ); | ||
errors[ element.name ] = previous.message = message; | ||
validator.invalid[ element.name ] = true; | ||
validator.showErrors( errors ); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it on purpose because of the line 1013 in this PR.
Should I omit it and change the line 1013 to the following: