Kendo网格自定义验证规则不起作用

时间:2014-01-27 20:10:57

标签: validation kendo-ui

我正在尝试将自定义验证规则与Kendo Web UI数据网格一起使用,但我无法使其正常工作。我能够将自定义规则附加到网格,并在用户离开网格单元格时调用它。规则函数也返回false以指示输入无效。但是从单元格中删除名称然后跳出显示后,不会显示错误消息。我错过了什么?

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 个答案:

答案 0 :(得分: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
                        }
                    }
                }
            }
        }
    });