2013-09-27 146 views
0

我想要使用基因敲除驗證插件做一些非常簡單的驗證。我想驗證,如果至少有一個文本字段有文本,並且至少有一個複選框被選中。所有的綁定工作正常,淘汰賽本身是非常棒的。我測試了本機驗證規則,他們使用消息傳遞。我只是無法驗證這兩條規則。基因敲除驗證 - 至少有一個字段有一個值,至少有一個複選框檢查

我知道我可以用jQuery很容易地檢查空值,但我真的很想利用淘汰賽。

(因爲我還沒有發現任何東西,但工作不驗證)模型:

var SearchForm = function(collections) { 

    // main search fields 
    this.fullRecord = ko.observable(); 
    this.title = ko.observable(); 
    this.author = ko.observable(); 

    // collections to search 
    var sources = []; 
    $.each(collections, function(index,collection) { 
     sources.push(new Source(collection)); 
    }); 

    this.sources = ko.observableArray(sources); 

    // Error handling vars 
    this.errors = ko.validation.group(this); 

}; 

var Source = function(collection) { 
    $.extend(this,collection); 
    this.id = "collection-"+this.code; 
    this.selected = ko.observable(true); 
}; 

在這裏,我只是創造從來自服務器收集數據源對象的列表。這些數據是無關緊要的,因爲我只關心可觀察的「選定」財產。

的標記:

<div id="advanced-controls" class="row"> 
    <div class="col-sm-8"> 
     <fieldset id="search-fields"> 
      <div class="form-group"> 
       <label for="fullrecord" class="control-label">Keywords:</label> 
       <input type="text" id="fullrecord" class="form-control" name="fullrecord" placeholder="Full Record Search" data-bind="value:fullRecord" /> 
      </div> 
      <div class="form-group"> 
       <label for="title" class="control-label">Title:</label> 
       <input type="text" id="title" name="title" class="form-control" data-bind="value:title"/> 
      </div> 
      <div class="form-group"> 
       <label for="author" class="control-label">Author:</label> 
       <input type="text" id="author" name="author" class="form-control" data-bind="value:author"/> 
      </div> 
      <div class="form-group"> 
       <button id="advanced-search-submit" class="btn btn-primary" data-bind="click:search">Search</button> 
       <button id="advanced-search-reset" class="btn" data-bind="click: clear">Clear All</button> 
      </div> 
     </fieldset> 
    </div> 
    <div class="col-sm-4"> 
     <fieldset data-bind="foreach: sources"> 
      <div class="form-group"> 
       <input type="checkbox" name="collections" data-bind="attr:{ id:id, value:code }, checked:selected, click: $parent.clearRequiredSourceError "> 
       <label data-bind="attr:{ for:id }, text: name"></label> 
      </div> 
     </fieldset> 
    </div> 
</div> 

在驗證功能在提交前:

// If there's any knockout validation errors 
    if (model.errors().length > 0) { 
     model.errors.showAllMessages(); 
     isValid = false; 
    }  

我試過的來源可觀測陣列這樣的設置自定義驗證擴展:

this.sources = ko.observableArray(sources).extend({ 
     validation: { 
      validator : function (sources) { 
       var anySelected = false; 
       $(sources).each(function(){ 
        anySelected = this.selected(); 
       }); 
       return anySelected; 
      }, 
      message: 'At least one source is required to search.' 
     } 
    }); 

但是,當單擊複選框時不會觸發,只有當數組是c掛起〜推,彈出等是的,我有配置正確設置:

ko.validation.configure({ 
     grouping: { 
      deep: true, 
      observable: true 
     } 
    }); 

這似乎應該是非常簡單的實現。也許我的大腦在本週進入整個淘汰賽世界的時候就被炸了。任何建議,非常感謝。提前致謝!

回答

2

原諒我不讀你的整個問題,因爲它是很長,但我很好奇,如果你需要淘汰賽驗證這個或如果你正在尋找這樣的事情 -

var selectedOption = ko.observable(); 
var selectionsOk = ko.computed(function() { 
    ((!!field1()|| !!field1()|| !!field1())&&!!selectedOption()) 
}); 

哪裏selectedOption是一個單選按鈕的列表,一旦選擇了一個,就會返回該值,並且可以使用observableArray來包含每個字段,使其成爲動態的,或者列出字段並確保其中至少有一個字段具有值。 !!!會評估你的觀察爲真或假,真會被退回,除非可觀的價值是nullundefined'',或false

計算的selectionOk可以用來防止點擊一些按鈕繼續或者相反的顯示錯誤消息,直到滿足條件。

+0

你說得對。單單使用敲除就很容易。我能夠用必要的驗證邏輯創建兩個獨立的計算布爾觀測值。觀察值明顯地綁定到唯一的錯誤消息。效果很好,謝謝你的提示! – headz68

+0

是!一個特殊的運算符還是雙重否定?它在這裏如何工作? –

+2

它將任何東西轉換爲布爾值。基本上所有不是null,undefined,''或false的東西都會返回true。 –

相關問題