2012-05-06 53 views
6

每當我使用require_from_group它會禁用所有其他驗證。任何想法爲什麼?Jquery .validate require_from_group

還有一種方法可以將「Telefon」和「Mobitel」分組並將require_from_group應用於它?

$(document).ready(function(){ 
    $("#fncMain").validate(
    { 
    /*groups:{Call:"Telefon Mobitel"},*/ 
    rules:{ 
     Davcna:{required:true,exactlength:5, digits:true}, 
     Idzav:{required:true,exactlength:5, digits:true}, 
     Maticna:{required:true,exactlength:5, digits:true}, 
     Telefon:{require_from_group: [1,".callme"]}, 
     Mobitel:{require_from_group: [1,".callme"]} 
    }, 
    messages:{ 

    }} 
    ); 
    }); 

此處未包含的所有其他字段都使用簡單的「必需」類。如果我刪除require_from_group適用於「Telefon」和「Mobitel」的規則,則所有其他字段驗證都可以正常工作。

感謝您的幫助。

編輯 HTML:http://cl.ly/29391q0Q3G231T2I380m(太長它張貼在這裏)

+1

您是否可以共享此表單的html,至少它與這些規則有關?即什麼事情有類'callme'等 – Ryley

+0

@Ryley我張貼我的HTML(表單部分)。檢查我的帖子的編輯。 :) – John

+0

我想你已經發現了一個bug ... – Ryley

回答

5

@Tats_innit發佈自定義require_from_group這裏:https://stackoverflow.com/posts/13127475

事實證明,這也解決了與版本發佈的記錄github上的bug require_from_group中的1.10.0在additional-method-1.10.js中對於jquery.validation

GitHub的問題: require_from_group disables other rules

smileyanp @ github上引用這篇文章在他的解決方案,他重用@ Tats_innit的功能,並創建了一個test那表明它工作正常並不會禁止其他規則驗證在require_from_group之前定義。

這篇文章是這裏,因爲這燒google搜索這樣一個小細節的3個小時,節省時間..

FIX:

只需更新additional-method-1.10.jsadditional-method-1.10.js加載後執行該代碼(覆蓋該功能)。

jQuery.validator.addMethod("require_from_group", function(value, element, options) { 
    var numberRequired = options[0]; 
    var selector = options[1]; 
    var fields = $(selector, element.form); 
    var filled_fields = fields.filter(function() { 
    // it's more clear to compare with empty string 
    return $(this).val() != ""; 
    }); 
    var empty_fields = fields.not(filled_fields); 
    // we will mark only first empty field as invalid 
    if (filled_fields.length < numberRequired && empty_fields[0] == element) { 
    return false; 
    } 
    return true; 
// {0} below is the 0th item in the options field 
}, jQuery.format("Please fill out at least {0} of these fields.")); 
+0

在比較此函數的1.11.1版本與此答案中的版本的行爲時,我注意到此版本似乎不像運輸版本那樣執行貪婪重新驗證。此版本在表單提交時重新驗證,但發貨人只要最小數量的字段中有值(onkeyup)就會清除驗證消息。僅供參考,以防任何人需要對其用例進行貪婪的重新驗證。 – jinglesthula

2

編輯:其實是這樣的修復已被納入1.12.0版本,你可以在這裏找到CDN指針是:http://jqueryvalidation.org/

以供參考:

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js 
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js 
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.js 
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/additional-methods.min.js 

在我找到上述解決方案之前,我發現下面的代碼,所以我的建議是使用上面引用的CDN鏈接,而不是將以下代碼粘貼到您的JS文件中。

有一個better fix out on GitHub now(滾動到最底部),我在這裏複製。這不是我的工作和編寫它的GitHub用戶sfreytag似乎並不是SO的貢獻者,我只是想把它放到SO中,所以其他找到這個的人不必通過線程挖掘GitHub的:

jQuery.validator.addMethod("require_from_group", function(value, element, options) { 
    var validator = this; 
    var selector = options[1]; 
    var validOrNot = $(selector, element.form).filter(function() { 
     return validator.elementValue(this); 
    }).length >= options[0]; 

    if(!$(element).data('being_validated')) { 
     var fields = $(selector, element.form); 
     fields.data('being_validated', true); 
     fields.valid(); 
     $(element.form).valid(); 
     fields.data('being_validated', false); 
    } 
    return validOrNot; 
}, jQuery.format("Please fill at least {0} of these fields.")); 

我已經做了有限的測試與此迄今,但它似乎是工作爲你所期望的,發生的所有驗證(而不是通過任何非「require_from_group」驗證作爲前吹),所以到目前爲止我對此感到滿意。我只是在我的JS代碼頂部的驗證器聲明之後添加它:

$.validator.setDefaults({ 
    debug: true, 
    success: "valid" 
}); 

jQuery.validator.addMethod("require_from_group", function(value, element, options) { 
    var validator = this; 
    var selector = options[1]; 
    //continuation of code...