2014-01-27 85 views
0

我試圖對Kendo Web UI數據網格使用自定義驗證規則,但是我一直無法讓它工作。我能夠將自定義規則附加到網格,並在用戶離開網格單元格時調用它。規則函數也返回false以指示輸入無效。但從單元中刪除名稱然後退出後,不顯示錯誤消息。我錯過了什麼?Kendo網格自定義驗證規則不起作用

的jsfiddle:http://jsfiddle.net/davidsalahi/qMRBc/

var validatorRules = { 
    rules: { 
     // This rule is executed when leaving a cell but the return value of false doesn't display any error message or prevent leaving the cell 
     customRule1: function (input) { 
      // OpCode must not be empty 
      if (input.attr("name") == "ProductName") { 
       return $.trim(input.val()) !== ""; 
      } 
     } 
    }, 
    messages: { 
     customRule1: "All fields are required" 
    } 
}; 

回答

1

您的數據源設置創建自定義的驗證規則,

我曾嘗試在你的jsfiddle和工作正常。

http://jsfiddle.net/pehlizm/qMRBc/4/

var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service", 
    dataSource = new kendo.data.DataSource({ 
     transport: { 
      read: { 
       url: crudServiceBaseUrl + "/Products", 
       dataType: "jsonp" 
      }, 
      parameterMap: function (options, operation) { 
       if (operation !== "read" && options.models) { 
        return { 
         models: kendo.stringify(options.models) 
        }; 
       } 
      } 
     }, 
     batch: true, 
     pageSize: 20, 
     schema: { 
      model: { 
       id: "ProductID", 
       fields: { 
        ProductID: { 
         editable: false, 
         nullable: true 
        }, 
        ProductName: {     //changes starts here 
         type: "string", 
         validation: { 
            custom: function(input) { 
             // set the custom message 
             input.attr("data-custom-msg", "Error"); 

              if (input.attr("name") == "ProductName") { 
              return $.trim(input.val()) !== ""; 
            } 
            } 
           } 
         },       //ends here 
        UnitPrice: { 
         type: "number", 
         validation: { 
          required: true, 
          min: 1 
         } 
        } 
       } 
      } 
     } 
    }); 
+0

嗨user2688655, 感謝您看我的jsfiddle。你是說它按原樣工作?沒有改變? –

+0

.Hi @DavidSalahi有變化。驗證部分已經改變了一點,它移到了模型設置中,我看到了 – MustafaP

+0

。感謝您的澄清。 –