尝试使 jquery 验证中的 addMethod 正常工作。 console.log 显示正确的响应,但始终无法通过验证

Trying to get addMethod in jquery validation to work. console.log shows the proper response, but it always fails validation

我正在为 jQuery 验证插件使用数据属性。我需要使用 "remote" 验证功能,但是,我需要根据使用的表单修改发送到远程位置的数据。我想使用数据属性而不是为每个表单创建 <script> 调用。

我创建了一个自定义 $.validator.addMethod 来处理这个问题,但每次我 运行 它,表单都会像返回 false 一样返回。我添加了对 console.log() 的调用,以确保数据根据远程脚本返回 truefalse,事实确实如此。它 returns true 在它应该的时候 false 在它应该的时候。

这是我的自定义添加方法:

$.validator.addMethod("dupcheck", function(value,element){
    var method = element.getAttribute("data-method");
    var data = { "dupcheckv" : value, "dupcheckm" : method };
    $.post( "duplicate-check.php", data).done(function(response){
        console.log(response);
        return response;
    });
});

我看过如何使jquery“$.post”请求同步[重复] ,但 async:false 已被弃用,因此没有帮助。

我通过复制所有 remote 函数并将其放入 addMethod 调用中使其工作:

$.validator.addMethod("dupcheck", function(value, element, param, method){
    if ( this.optional( element ) ) {
        return "dependency-mismatch";
    }

    method = typeof method === "string" && method || "dupcheck";

    var previous = this.previousValue( element, method ),
        validator, data, optionDataString;

    if ( !this.settings.messages[ element.name ] ) {
        this.settings.messages[ element.name ] = {};
    }
    previous.originalMessage = previous.originalMessage || this.settings.messages[ element.name ][ method ];
    this.settings.messages[ element.name ][ method ] = previous.message;

    param = typeof param === "string" && { url: param } || param;
    optionDataString = $.param( $.extend( { data: value }, param.data ) );
    if ( previous.old === optionDataString ) {
        return previous.valid;
    }

    previous.old = optionDataString;
    validator = this;
    this.startRequest( element );
    data = {};
    data[ "dupcheckv" ] = value;
    data[ "dupcheckm" ] = element.getAttribute("data-method");
    $.ajax( $.extend( true, {
        mode: "abort",
        port: "validate" + element.name,
        dataType: "json",
        method: "POST",
        data: data,
        context: validator.currentForm,
        success: function( response ) {
            var valid = response === true || response === "true",
                errors, message, submitted;

            validator.settings.messages[ element.name ][ method ] = previous.originalMessage;
            if ( valid ) {
                submitted = validator.formSubmitted;
                validator.resetInternals();
                validator.toHide = validator.errorsFor( element );
                validator.formSubmitted = submitted;
                validator.successList.push( element );
                validator.invalid[ element.name ] = false;
                validator.showErrors();
            } else {
                errors = {};
                message = response || validator.defaultMessage( element, { method: method, parameters: value } );
                errors[ element.name ] = previous.message = message;
                validator.invalid[ element.name ] = true;
                validator.showErrors( errors );
            }
            previous.valid = valid;
            validator.stopRequest( element, valid );
        }
    }, param ) );

    return "pending";
});