Kendo-Grid column field validation

∥☆過路亽.° 提交于 2019-12-07 05:28:30

问题


I am working on populating kendo--grid with APIs data but on adding validation on one field is automatically working for every other fields too.

Here is schema inside 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'}
                       }
                   }
                },  

Here I have added validation for title field but its getting called for others fields too. I am adding one snapshot of validation---

Please help me to find errors in it.


回答1:


There isn't really any error in your code, but more like an error in Kendo Grid's validation design. Even though you specify the validation function only in the title field, it will run the validation globally for any input field that you edit.

In validateTitle you need to filter which input you want the validating function to run on. Something like this:

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

If you need a live working demo, you can always refer to Telerik's online demos that are editable, very handy for playing around with things. Here's the demo for custom validation where they similarly have to filter the input for the field name.




回答2:


you want simply required field validation means just add your view model property attributes

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



回答3:


We can easily set the maximum length using this code,It will not allow user to enter more characters than the specified one

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


来源:https://stackoverflow.com/questions/24671452/kendo-grid-column-field-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!