jquery validation - remote method won't trigger after valid

后端 未结 2 1676
心在旅途
心在旅途 2020-12-11 02:25
email1: {
                    required: true,
                    blacklist: true,
                    beanEmailValidator: true,
                    minlength: 6,
           


        
相关标签:
2条回答
  • 2020-12-11 03:02

    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

    0 讨论(0)
  • 2020-12-11 03:07

    I had the same symptoms, but it was caused by a different problem.

    It looks like the jQuery remote validation method will not trigger another ajax call if it believes the field's value hasn't changed. This was problematic for me because I have remote validation that validates on two fields in the form: mobile number (IE 555-123-1234) and country code (IE 1 for US).

    If the mobile number does not change but the country code does, we need to re-validate. However, the plugin doesn't make another ajax call if the validation is bound to the mobile number field because its value hasn't changed.

    You can see where the plugin does this caching in the remote method:

    if ( previous.old === optionDataString ) {
        return previous.valid;
    }
    

    The plugin stores the previous value using jQuery's $data method and the "previousValue" key. Therefore, in my case all I had to do was clear this value:

    $("#my-phone-number-element").removeData("previousValue");
    $("#my-phone-number-element").valid();
    
    0 讨论(0)
提交回复
热议问题