使用jQuery Validate暂时禁用规则

时间:2011-10-10 14:58:52

标签: jquery jquery-validate

我的表单有几个相当复杂的验证规则,我必须暂时禁用给定规则,以便稍后启用它。删除它然后再添加它似乎毫无意义所以我需要一个工具来禁用和启用验证规则。

2 个答案:

答案 0 :(得分:3)

从版本1.8.1的jquery.validate.js开始,我在2个案例中添加了“rules”函数,以便命令switch语句如下所示。

switch(command) {
case "add":
    $.extend(existingRules, $.validator.normalizeRule(argument));
    staticRules[element.name] = existingRules;
    if (argument.messages)
        settings.messages[element.name] = $.extend( settings.messages[element.name], argument.messages );
    break;
case "remove":
    if (!argument) {
        delete staticRules[element.name];
        return existingRules;
    }
    var filtered = {};
    $.each(argument.split(/\s/), function(index, method) {
        filtered[method] = existingRules[method];
        delete existingRules[method];
    });
    return filtered;
case "disable":
    if (!argument) {
        return false;
    }
    $.each(argument.split(/\s/), function(index, method) {
        existingRules[method].disabled=true;
    });
    break;
case "enable":
    if (!argument) {
        return false;
    }
    $.each(argument.split(/\s/), function(index, method) {
        existingRules[method].disabled=false;
    });
    break;
}

然后为了实现这一点,在“check”函数中我们添加一个条件来检查我们设置的这个禁用参数的状态。值得注意的是,如果规则从未被禁用/启用,那么该值将是未定义的,我们在此假设如果未定义,我们对规则感到满意。此函数的循环语句现在以:

开头
for (var method in rules ) {
    var rule = { method: method, parameters: rules[method] };
    try {
        if (typeof rule.parameters["disabled"] === 'undefined' || rule.parameters["disabled"] == false)
        {
            var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );
        } else {
            continue;
        }

就像使用“添加”和“删除”设施一样使用它,例如

$('#email_address').rules("disable", "remote");
$('#email_address').rules("enable", "remote");

我希望这对某人有所帮助,我一直在靠墙试图到达这里。

答案 1 :(得分:0)

我无法让您的代码工作,所以我做了一些尝试,以下内容将允许您启用/禁用特定字段的验证。希望这对某人有所帮助。

在 jquery 验证中的第 163 行将代码替换为:(添加启用和禁用规则)

if ( command ) {
    settings = $.data( element.form, "validator" ).settings;
    staticRules = settings.rules;
    existingRules = $.validator.staticRules( element );
    switch ( command ) {
    case "add":
        $.extend( existingRules, $.validator.normalizeRule( argument ) );

        // Remove messages from rules, but allow them to be set separately
        delete existingRules.messages;
        staticRules[ element.name ] = existingRules;
        if ( argument.messages ) {
            settings.messages[ element.name ] = $.extend( settings.messages[ element.name ], argument.messages );
        }
        break;
    case "remove":
        if ( !argument ) {
            delete staticRules[ element.name ];
            return existingRules;
        }
        filtered = {};
        $.each( argument.split( /\s/ ), function( index, method ) {
            filtered[ method ] = existingRules[ method ];
            delete existingRules[ method ];
        } );
        return filtered;
        break;
    case "disable":
         existingRules['disabled'] = true;
        break;
    case "enable":
        delete existingRules['disabled'];
        break;
    }
}

并在第 803 行替换以下代码:(忽略禁用规则)

for ( method in rules ) {
    rule = { method: method, parameters: rules[ method ] };
     try {
         if ('disabled' in rules){
             continue;
         }
         result = $.validator.methods[ method ].call( this, val, element, rule.parameters );
         
        // If a method indicates that the field is optional and therefore valid,
        // don't mark it as valid when there are no other rules
        if ( result === "dependency-mismatch" && rulesCount === 1 ) {
            dependencyMismatch = true;
            continue;
        }
        dependencyMismatch = false;

        if ( result === "pending" ) {
            this.toHide = this.toHide.not( this.errorsFor( element ) );
            return;
        }

        if ( !result ) {
            this.formatAndAdd( element, rule );
            return false;
        }
    } catch ( e ) {
        if ( this.settings.debug && window.console ) {
            console.log( "Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.", e );
        }
        if ( e instanceof TypeError ) {
            e.message += ".  Exception occurred when checking element " + element.id + ", check the '" + rule.method + "' method.";
        }

        throw e;
    }
}

这将允许您禁用和启用验证规则,就像这样。

$('input[name="test"]').rules('enable');
$('input[name="test"]').rules('disable');