【问题标题】:Kendo-Grid column field validationKendo-Grid 列字段验证
【发布时间】:2014-08-31 12:40:13
【问题描述】:

我正在使用 API 数据填充 kendo--grid,但在一个字段上添加验证也会自动适用于其他所有字段。

这是 kendo-dataSource 中的架构:

schema: {
                   model: {
                       id : "id",
                       fields: {
                           id: { editable: false, type: 'number'},
                           name: { editable: true, type : "string" },
                           unique_url: { editable: true , type: 'string'},
                           image_url : { editable: true, type : "string" },
                           title: {type : "string", validation: {
                                                required: true,
                                                validateTitle: function (input) {
                                                    console.log("I am inside validation",input.val());
                                                    if (input.val().length > 5) {
                                                       input.attr("data-validateTitle-msg", "Max length exceeded 5 characters only");
                                                       return false;
                                                    }    

                                                    return true;
                                                }
                                            }
                                            },
                           body: { editable: true, type : "string",validation: { max: 90, required: true, message : "Maximum characters should be 90"} },
                           adaccount_id: { editable: false, type: 'number'}
                       }
                   }
                },  

在这里,我添加了对标题字段的验证,但也对其他字段进行了调用。 我正在添加一张验证快照---

请帮我找出其中的错误。

【问题讨论】:

    标签: validation kendo-ui kendo-grid kendo-dataviz


    【解决方案1】:

    您的代码实际上并没有任何错误,但更像是 Kendo Grid 的验证设计中的错误。即使您仅在 title 字段中指定验证函数,它也会对您编辑的任何输入字段全局运行验证。

    validateTitle 中,您需要过滤您希望验证功能在哪个输入上运行。像这样的:

    if (input.is("[name='title']") && input.val().length > 5) {
        input.attr("data-validateTitle-msg", "Max length exceeded 5 characters only");
        return false;
    }
    

    如果您需要实时工作演示,您可以随时参考 Telerik 的在线演示,这些演示是可编辑的,非常便于玩弄。这是用于自定义验证的demo,他们同样必须过滤字段名称的输入。

    【讨论】:

    • “Kendo Grid 的验证设计中的一个错误”——你把它钉在了头上!设计完全崩溃了!
    【解决方案2】:

    您想要简单的必填字段验证意味着只需添加您的视图模型属性属性

    [Required(ErrorMessage ="CountryCode is Mandatory")]
            public virtual string CountryCode
            {
                get;
                set;
            }
    

    【讨论】:

      【解决方案3】:

      我们可以使用此代码轻松设置最大长度,它不允许用户输入超过指定字符的字符

        model: {
                          id: "CLASSID",
                          fields: {
                              CLASSID: { type: "number" },
                              CLSNAME: { type: "string" },
                              CLSFLAG: {
                                  type: "string", validation: {
                                      required: true,maxlength:"3"
                                  }
                              },
                              CLSSTATUS: { type: "boolean" }
                          }
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-23
        相关资源
        最近更新 更多