【问题标题】:Kendo Grid Custom Validation Rules Not WorkingKendo Grid 自定义验证规则不起作用
【发布时间】:2014-01-27 20:10:57
【问题描述】:

我正在尝试将自定义验证规则与 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"
    }
};

【问题讨论】:

    标签: validation kendo-ui


    【解决方案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
                            }
                        }
                    }
                }
            }
        });
    

    【讨论】:

    • 您好 user2688655,感谢您查看我的 jsfiddle。你是说它按原样工作吗?没有变化?
    • 。嗨@DavidSalahi 有变化。验证部分发生了一些变化,它移到了模型设置中,
    • 我明白了。感谢您的澄清。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多