2014-11-21 49 views
1

我現在有類似於下面的一種形式:劍道UI驗證 - 具有相同名稱處理輸入屬性

<form action="/" method="post" id="myForm"> 
    <div class="row"> 
     <input type="text" name="rowValue" class="rowValue"> 
    </div> 
    <div class="row"> 
     <input type="text" name="rowValue" class="rowValue"> 
    </div> 
    <div class="row"> 
     <input type="text" name="rowValue" class="rowValue"> 
    </div> 
    <div class="row"> 
     <input type="text" name="rowValue" class="rowValue"> 
    </div> 
    <input type="submit" value="Submit"> 
</form> 

一點背景:JS用於注入的新的「行」 X量成形式。

我試着使用:

var myForm = $('#myForm').kendoValidator({ 
    /* rules/messages go here*/ 
}).data('kendoValidator'); 

myForm.validate(); 

我只得到一個錯誤信息顯示在第一input[name='rowValue']起來。

JS Fiddle

我懷疑是劍道驗證需要唯一的名稱屬性正確驗證。這很遺憾,因爲很多後端語言都可以接受相同的名稱屬性,因爲它們連接值或將它們轉換爲數組或集合(ASP.NET)。

有沒有辦法讓Kendo UI Validator驗證具有相同名稱屬性的表單域?

回答

1

您的懷疑是正確的。你可以調整爲驗證你的使用情況是這樣的:

kendo.ui.Validator.prototype.validateInput = function (input) { 
    input = $(input); 

    var that = this, 
     template = that._errorTemplate, 
     result = that._checkValidity(input), 
     valid = result.valid, 
     className = ".k-invalid-msg", 
     fieldName = (input.attr("name") || ""), 
     lbl = input.parent().find("span" + className).hide(), 
     messageText; 

    input.removeAttr("aria-invalid"); 

    if (!valid) { 
     messageText = that._extractMessage(input, result.key); 
     that._errors[fieldName] = messageText; 
     var messageLabel = $(template({ 
      message: messageText 
     })); 

     that._decorateMessageContainer(messageLabel, fieldName); 

     if (!lbl.replaceWith(messageLabel).length) { 
      messageLabel.insertAfter(input); 
     } 
     messageLabel.show(); 

     input.attr("aria-invalid", true); 
    } 
    input.toggleClass("k-invalid", !valid); 

    return valid; 
}; 

注意,有在此方法中的很少的簡化,所以它可能在某些極端情況打破。

demo

+0

GREAT解決方案。不知道爲什麼我沒有想到這一點。謝謝你的幫助! – 2014-11-28 18:11:01