解决jQuery-validation框架remote校验只能返回true、false问题

    默认jQuery-validation框架的remote远程校验只支持true、false返回值,不能满足那些需要返回具体冲突原因的特殊业务场景,
例如:数据重复,需要返回重复的那条数据的id,这时就需要返回自定义类型。
    修改方案:
    1> 将datatype由json修改为text这样可以传其他类型;
    2> 定义一个封装bean,新建一个js文件重写掉原生的remote方法,修改success函数,当返回值为json object时,修改error显示message从bean中获取(推荐)

重点修改success函数:
(建议新建一个js文件重写掉jQuery-validation框架的remote函数,这个即使升级版本也不会有影响)
if(typeof response == 'object') {
        message = response.message || validator.defaultMessage( element, "remote" );
}


jQuery.extend( jQuery.validator.methods, {

    // http://jqueryvalidation.org/remote-method/
    remote: function( value, element, param ) {
        if ( this.optional( element ) ) {
            return "dependency-mismatch";
        }

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

        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;

        param = typeof param === "string" && { url: param } || param;

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

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

                validator.settings.messages[ element.name ].remote = previous.originalMessage;
                if ( valid ) {
                    submitted = validator.formSubmitted;
                    validator.prepareElement( element );
                    validator.formSubmitted = submitted;
                    validator.successList.push( element );
                    delete validator.invalid[ element.name ];
                    validator.showErrors();
                } else {
                    errors = {};
                  if(typeof response == 'object') {
                        message = response.message || validator.defaultMessage( element, "remote" );
                  } else {
                        message = response || validator.defaultMessage( element, "remote" );
                   }
                    errors[ element.name ] = previous.message = $.isFunction( message ) ? message( value ) : message;
                    validator.invalid[ element.name ] = true;
                    validator.showErrors( errors );
                }
                previous.valid = valid;
                validator.stopRequest( element, valid );
            }
        }, param ) );
        return "pending";
    }
});


你可能感兴趣的:(Web,re,remote,ajax)