Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
888 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...