Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validation - remote method won't trigger after valid

email1: {
                    required: true,
                    blacklist: true,
                    beanEmailValidator: true,
                    minlength: 6,
                    maxlength: 70,
                    remote: {
                        type: "GET",
                        url: llbFieldValJSONURL,
                        data: {
                              fieldData : function() {
                                return $("#enterEmail").val();
                              }                           
                         },
                        dataType:"json",
                        dataFilter: function(data) {
                            var json = jQuery.parseJSON(data);
                            if(json.error == "true") {
                                return "\"" + json.errorMessage + "\"";
                            } else {
                                return success;

                            }

                        }
                    }

                }

This function check the uniqueness of a username and returns an error if not unique. The issue is that the remote method won't make any additional calls after being valid.. I need it to trigger every time a value is entered/changed.

For example, if I enter 3 non-unique usernames, the call is made every time. If I enter a unique username, the call is made correctly and the username comes back as valid. So the field is then valid. Now if I enter another non-unique (invalid) username, the remote method won't trigger again.

Wondering if it's somehow caching the response?

invalid (non-unique) response:

{"error":"true","errorMessage":"<b>The User Name you chose is already in use.</b>  Please enter another name."}

valid (unique) response:

{"error":"false","errorMessage":""}

Edit

Saw this on SO:

https://stackoverflow.com/a/1821667/335514

$("#site").change(function() {
  $("#email").removeData("previousValue");
});

So the plugin seems to be caching the response. Does the plugin have a method to switch off caching, or would adding a cache: false to the ajax call do the same thing?

Edit

Neither of these methods worked. It appears that once a field is marked valid, it won't make the remote call again. Thoughts on how to fix? Would putting the function into it's own addMethod prevent this scenario?

like image 741
Jason Avatar asked Jul 13 '12 22:07

Jason


1 Answers

UPDATE/SOLUTION:

Looking at the source code of jquery.validate.js, particularly this snippet:

success: function(response) {
    validator.settings.messages[element.name].remote = previous.originalMessage;
    var valid = response === true;

    if ( valid ) {   ...

Since the response is evaluated as JSON, you are expected to call return "true". This is also pointed out in the documentation

"The response is evaluated as JSON and must be true for valid elements"

However, it can be quite confusing when looking at the source code that does an exactly equals test for true, if you neglect the fact that the following is implicitly set dataType: json

end update


Upon investigating some more, I'd say that your 'dataFilter' should not be returning 'success'

Refer to Callback function queues @ http://api.jquery.com/jQuery.ajax/

dataFilter callback is invoked immediately upon successful receipt of response data. It receives the returned data and the value of dataType, and must return the (possibly altered) data to pass on to success.

My guess is that your JS breaks on trying to do return success and thus no further validation requests are sent

like image 149
ryuusenshi Avatar answered Nov 16 '22 01:11

ryuusenshi