2014-12-03 120 views
0

我正在使用require_from_group進行驗證,但它不起作用。jQuery驗證require_from_group不工作

我的腳本代碼是在這裏:

jQuery.validator.setDefaults({ 調試:真實, $(文件)。就緒(函數(){

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

$("#findproject_form").validate({ 
    rules: { 
     Profession: { 
      require_from_group: [1, ".validategroup"] 
     }, 
     Location1: { 
      require_from_group: [1, ".validategroup"] 
     }, 
     Company: { 
      require_from_group: [1, ".validategroup"] 
     }, 
     Name: { 
      require_from_group: [1, ".validategroup"] 
     }, 
     KeyWord: { 
      require_from_group: [1, ".validategroup"] 
     } 
    } 
}); 

$.validator.addMethod("require_from_group", function (value, element, options) { 
    var $fields = $(options[1], element.form), 
     $fieldsFirst = $fields.eq(0), 
     validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this), 
     isValid = $fields.filter(function() { 
      return validator.elementValue(this); 
     }).length >= options[0]; 

    // Store the cloned validator for future validation 
    $fieldsFirst.data("valid_req_grp", validator); 

    // If element isn't being validated, run each require_from_group field's validation rules 
    if (!$(element).data("being_validated")) { 
     $fields.data("being_validated", true); 
     $fields.each(function() { 
      validator.element(this); 
     }); 
     $fields.data("being_validated", false); 
    } 
    return isValid; 
}, $.validator.format("Please fill at least {0} of these fields.")); 

});

當任何一個或多個字段爲fill_out時,它會給出錯誤,而不是當它爲空並且沒有到達動作頁面時 那麼這有什麼問題

我的形式IS HERE

<form id="findproject_form" method="post" action="{$BASE_URL}findproject" > 
     <div class="bgtrans"><h3>Search By</h3> 
     <div class="div_bg1"> 
     <div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Profession" id="Profession" name="Profession" value="{if $profession}{$profession|escape}{/if}" /></div></div> 
     <div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Location" id="Location1" name="Location1" value="{if $location}{$location|escape}{/if}" /></div></div> 
     <div class="searchbg" style="margin-right:0px;"> 
     <div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Company" id="Company" name="Company" value="{if $company}{$company|escape}{/if}"/></div> 

     </div> 

     <div class="clear"></div> 
     </div> 
     <div class="div_bg2"> 
     <div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Name" id="Name" name="Name" value="{if $name}{$name|escape}{/if}" /></div></div> 
     <div class="searchbg"><div class="seachbginputbg"><input class="seachbginput validategroup" type="text" placeholder="Key Words" id="KeyWord" name="KeyWord" value="{if $keyword}{$keyword|escape}{/if}"/></div></div> 
     <div class="searchbg" style="margin-right:0px;"> 
     <input class="bgbttn" type="submit" name="submit" value="" /> 
     </div> 
     <div class="clear"></div> 
     </div>    
     </div> 

+0

你不需要試圖驗證反對前增加你的方法是什麼? – rjdown 2014-12-03 12:21:43

+0

如果我刪除它給出錯誤:檢查元素Location1時發生異常,請檢查'require_from_group'方法 – 2014-12-03 12:22:59

+0

這不是** require_from_group函數... [這只是'addMethod'文檔中的示例代碼](http ://jqueryvalidation.org/jQuery.validator.addMethod/)。 – Sparky 2014-12-03 23:49:42

回答

1

刪除自定義方法。(這讓現場可選的,它是你的字段的值進行比較,爲您的參數的總和。由於您的參數個數1和類名,顯然,這是沒有意義的。) ...

jQuery.validator.addMethod("require_from_group", function(value, element, params) { 
    return this.optional(element) || value == params[0] + params[1]; 
}, jQuery.validator.format("Please fill out atleast one field ")); 

這沒有意義,因爲那不是require_from_group函數... that's just some sample code from the .addMethod() documentation的名稱更改爲"require_from_group"


使用包括在the additional-methods.js filerequire_from_group方法/規則的默認版本。您可以包含整個文件或複製下面的一個方法。

正如你所看到的,這不是一個微不足道的功能。

$.validator.addMethod("require_from_group", function(value, element, options) { 
    var $fields = $(options[1], element.form), 
     $fieldsFirst = $fields.eq(0), 
     validator = $fieldsFirst.data("valid_req_grp") ? $fieldsFirst.data("valid_req_grp") : $.extend({}, this), 
     isValid = $fields.filter(function() { 
      return validator.elementValue(this); 
     }).length >= options[0]; 

    // Store the cloned validator for future validation 
    $fieldsFirst.data("valid_req_grp", validator); 

    // If element isn't being validated, run each require_from_group field's validation rules 
    if (!$(element).data("being_validated")) { 
     $fields.data("being_validated", true); 
     $fields.each(function() { 
      validator.element(this); 
     }); 
     $fields.data("being_validated", false); 
    } 
    return isValid; 
}, $.validator.format("Please fill at least {0} of these fields.")); 

DEMO(與你的jQuery和HTML):http://jsfiddle.net/y3qayufu/2/

+0

我在additional-methods.js中添加了上述方法,但仍然收到相同的錯誤 – 2014-12-04 05:30:48

+0

@HarshGandhi,對我來說工作正常... http://jsfiddle.net/y3qayufu/ – Sparky 2014-12-04 06:25:15

+0

我更新了代碼,但它不工作。我也在這裏上傳代碼。我在smarty模板文件 – 2014-12-04 09:48:16