Why is the wrong field value shown in the error message when using jQuery (remote) validation?

前端 未结 2 877
暗喜
暗喜 2021-01-28 04:51

I\'m using the jQuery validation plugin in a very similar manner to the Remember The Milk demo.

$(\"#registrationForm\").validate({
  rules: {      
    email: {         


        
相关标签:
2条回答
  • 2021-01-28 05:30

    Over two years later and the bug still doesn't seem to be fixed, so here's what I found:

    The issue is in the remote function:

    remote: function(value, element, param) {
        if ( this.optional(element) )
            return "dependency-mismatch";
    
        var previous = this.previousValue(element);
        if (!this.settings.messages[element.name] )
            this.settings.messages[element.name] = {};
        previous.originalMessage = this.settings.messages[element.name].remote;
        this.settings.messages[element.name].remote = previous.message;
    
        // snip more code...
    }
    

    Trouble is that on the 2nd and later evaluations of this function, the messages[element.name].remote contains a specific (text) error message, and the actual original message is lost forever when previous.originalMessage is overwritten after that.

    I was able to work around the issue by adding a check before that line:

    if (!previous.originalMessage)
        previous.originalMessage = this.settings.messages[element.name].remote;
    

    I'm not sure if this is really the correct solution, but it works - validation failures now reapply the format function on each validation failure.

    0 讨论(0)
  • 2021-01-28 05:52

    The user name field on the Remember the Milk demo fails in the same way (when entering user names 'Peter' and 'George'), so you've probably found a bug in the plugin.

    0 讨论(0)
提交回复
热议问题