Side navigation
#3807 closed bug (fixed)
Opened January 07, 2009 08:56PM UTC
Closed June 10, 2009 11:56AM UTC
[validate] remote method, custom messages
Reported by: | joern | Owned by: | joern |
---|---|---|---|
Priority: | major | Milestone: | |
Component: | plugin | Version: | |
Keywords: | validate remote | Cc: | |
Blocked by: | Blocking: |
Description
It looks like the implementation of the remote method is supposed to
support custom messages from the server when using the remote method
but because of a trivial bug, they don't work.
This quick change fixes the problem (details below)
From: 915 success: function(response) { 916 if ( response ) { 917 var submitted = validator.formSubmitted; To: 915 success: function(response) { 916 if ( response === true ) { 917 var submitted = validator.formSubmitted;
Because of type coercion, we can only see whether the response is
"truthy" or "falsey" from this line. If we take a look at line 924:
924 errors[element.name] = response || validator.defaultMessage( element, "remote" );
It would seem to be checking the response and assigning that to the
error message if it is truthy, but it never can be because of the
coercion on line 916. Explicitly checking for true in the response
allows us to provide a string as the response and automatically see
this appear as the error message. This gives the remote response three
possible cases:
true: the validation passed
false: the validation did not pass - display the default error
message / message provided to the validate method
<string>: the validation did not pass, display this string as the
custom error
Given that it's only possible to have one instance of remote on each
field, this is a useful feature for systems that need to do 2 or more
distinct checks on a field at the server end.
Attachments (0)
Change History (2)
Changed January 07, 2009 08:57PM UTC by comment:1
description: | {{{ \ It looks like the implementation of the remote method is supposed to \ support custom messages from the server when using the remote method \ but because of a trivial bug, they don't work. \ \ This quick change fixes the problem (details below) \ \ From: \ 915 success: function(response) { \ 916 if ( response ) { \ 917 var submitted = validator.formSubmitted; \ \ To: \ 915 success: function(response) { \ 916 if ( response === true ) { \ 917 var submitted = validator.formSubmitted; \ \ Because of type coercion, we can only see whether the response is \ "truthy" or "falsey" from this line. If we take a look at line 924: \ \ 924 errors[element.name] = response || validator.defaultMessage \ ( element, "remote" ); \ \ It would seem to be checking the response and assigning that to the \ error message if it is truthy, but it never can be because of the \ coercion on line 916. Explicitly checking for true in the response \ allows us to provide a string as the response and automatically see \ this appear as the error message. This gives the remote response three \ possible cases: \ true: the validation passed \ false: the validation did not pass - display the default error \ message / message provided to the validate method \ <string>: the validation did not pass, display this string as the \ custom error \ \ Given that it's only possible to have one instance of remote on each \ field, this is a useful feature for systems that need to do 2 or more \ distinct checks on a field at the server end. \ }}} → It looks like the implementation of the remote method is supposed to \ support custom messages from the server when using the remote method \ but because of a trivial bug, they don't work. \ \ This quick change fixes the problem (details below) \ {{{ \ From: \ 915 success: function(response) { \ 916 if ( response ) { \ 917 var submitted = validator.formSubmitted; \ \ To: \ 915 success: function(response) { \ 916 if ( response === true ) { \ 917 var submitted = validator.formSubmitted; \ }}} \ Because of type coercion, we can only see whether the response is \ "truthy" or "falsey" from this line. If we take a look at line 924: \ {{{ \ 924 errors[element.name] = response || validator.defaultMessage( element, "remote" ); \ }}} \ It would seem to be checking the response and assigning that to the \ error message if it is truthy, but it never can be because of the \ coercion on line 916. Explicitly checking for true in the response \ allows us to provide a string as the response and automatically see \ this appear as the error message. This gives the remote response three \ possible cases: \ true: the validation passed \ false: the validation did not pass - display the default error \ message / message provided to the validate method \ <string>: the validation did not pass, display this string as the \ custom error \ \ Given that it's only possible to have one instance of remote on each \ field, this is a useful feature for systems that need to do 2 or more \ distinct checks on a field at the server end. |
---|
Changed June 10, 2009 11:56AM UTC by comment:2
resolution: | → fixed |
---|---|
status: | new → closed |
Fixed in r6385.