Bug Tracker

Opened 12 years ago

Last modified 11 years ago

#3807 closed bug

[validate] remote method, custom messages — at Initial Version

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.

Change History (0)

Note: See TracTickets for help on using tickets.