Bug Tracker

Ticket #3807 (closed bug: fixed)

Opened 6 years ago

Last modified 6 years ago

[validate] remote method, custom messages

Reported by: joern Owned by: joern
Priority: major Milestone:
Component: plugin Version:
Keywords: validate remote Cc:
Blocking: Blocked by:

Description (last modified by joern) (diff)

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

comment:1 Changed 6 years ago by joern

  • Description modified (diff)

comment:2 Changed 6 years ago by joern

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in r6385.

Note: See TracTickets for help on using tickets.